The short range field validator in Struts is very similar to the integer one, except it can be used only for a field of short primitive/wrapper type. This validator checks if a short field falls within a certain range. Like other Struts validators, there are two forms of using this validator:

NOTES: Attempting to apply the short validator on a field of other types (e.g. long, int, or byte) will result in a runtime java.lang.ClassCastException.

 

1. Struts Short Range Field Validator XML

Usage:

 

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:

 

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:

 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.