Struts Short Range Field Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
- XML: using type=”short” attribute in <validator> or <field-validator> elements.
- Annotation: using @ShortRangeFieldValidator annotation type before field’s setter method or action method (plain-validator).
1. Struts Short Range Field Validator XML
Usage:- Field-validator syntax:
<field name="fieldName"> <field-validator type="short"> <param name="param name">param value</param> <message>validation error message</message> </field-validator> </field>
- Plain-validator syntax:
<validator type="short"> <param name="param name">param value</param> <message>validation error message</message> </validator>
Parameters:
Parameter name | Required | Description |
fieldName | Name of the field (required if using plain validator syntax). | |
min | No | The minimum value to check. It won’t be checked if omitted. |
max | No | The maximum value to check. It won’t be checked if omitted. |
parse | No | Boolean value. If set to true, evaluate the minExpressionand maxExpression to find min/max. |
minExpression | No | Expression to calculate the minimum value. It won’t be evaluated if omitted. |
maxExpression | No | Expression to calculate the maximum value. It won’t be evaluated if omitted. |
Struts Short Range Field Validator XML Examples:
- Field-validator example:
<field name="quantity"> <field-validator type="short"> <param name="min">1</param> <param name="max">10</param> <message>Please enter quantity between ${min} and ${max}</message> </field-validator> </field>
- Omitting the minimum check:
<field name="quantity"> <field-validator type="short"> <param name="max">10</param> <message>Please enter quantity less than ${max}</message> </field-validator> </field>
- Using minExpression and maxExpression:
<field name="quantity"> <field-validator type="short"> <param name="minExpression">${minValue}</param> <param name="maxExpression">${maxValue}</param> <message>Please enter quantity between ${min} and ${max}</message> </field-validator> </field>
In this case, we have to implement two methods getMinValue() and getMaxValue() in the action or JavaBean class as follows:public short getMinValue() { return 1; } public short getMaxValue() { return 10; }
- Plain-validator example:
<validator type="short"> <param name="fieldName">quantity</param> <param name="min">1</param> <param name="max">10</param> <message>Oops! Please enter quantity between ${min} and ${max}</message> </validator>
2. Struts @ShortRangeFieldValidator Annotation
Usage: Put the @ShortRangeFieldValidatorannotation before the setter method or action method (in case of using plain-validator) in the following form:@ShortRangeFieldValidator(param1 = "param 1 value", param2 = "param 2 value", ...)
Parameters:
Parameter name | Required | Default value | Description |
message | Yes | validation error message. | |
key | No | i18n key for validation error message. | |
messageParams | No | Additional parameters to customize the message. | |
fieldName | No | Specifies field name in case this validator type is plain-validator. | |
shortCircuit | No | false | Whether this validator is short circuit. |
type | No | ValidatorType.FIELD | type of the validator: field-validator (FIELD) or plain-validator (SIMPLE). |
min | No | The minimum value to check. | |
minExpression | No | OGNL expression used to calculate the minimum value. | |
max | No | The maximum value to check | |
maxExpression | No | OGNL expression used to calculate the maximum value. | |
parse | No | false | Whether to evaluate the minExpressionand maxExpression to find min/max. |
Struts @ShortRangeFieldValidator Annotation Examples:
- Basic field-validator:
@ShortRangeFieldValidator( min = "20", max = "70", message = "Please enter quantity between ${min} and ${max}") public void setQuantity(short quantity) { this.quantity = quantity; }
- Specifying i18n key for the message:
@ShortRangeFieldValidator( min = "20", max = "70", message = "Default message", key = "form.validation.quantity") public void setQuantity(short quantity) { this.quantity = quantity; }
- Plain-validator (annotating the action method):
@ShortRangeFieldValidator( type = ValidatorType.SIMPLE, fieldName = "quantity", min = "20", max = "70", message = "Please enter quantity between ${min} and ${max}" ) public String execute() { return SUCCESS; }
- Struts Form Handling Tutorial
- Struts Form Validation Tutorial
- Struts Date Range Field Validator Example
- Struts Integer Range Field Validator Example
- Struts String Length Field Validator Example
- Struts URL Validator Example
- Struts Required Field Validator Example
- Struts Field Expression Validator Example
Other Struts Tutorials:
- Introduction to Struts 2 framework
- Struts beginner tutorial (Eclipse + Tomcat + XML)
- Struts Beginner Tutorial with Annotations
- Struts beginner tutorial with Convention Plugin (zero-configuration)
- How to handle exceptions in Struts
- Send e-mail with attachments in Struts
- Struts File Upload Tutorial
- Struts - Spring - Hibernate Integration Tutorial
Comments