The expression validator is a Struts non-field validator which can be used to validate the relations among fields (or any variables in the value stack) using OGNL expression, rather than validating a single field. For example, checking if an integer field is greater than another, or checking if a date field must be after another. This validator can be used in either of the following forms:

1. Struts Expression Validator XML

Because this is a non-field validator so it can be only used with the plain-validator syntax as follows:

<validator type="expression">
	<param name="expression">OGNL expression</param>
	<message>validation error message</message>
</validator> 

Parameters:

Parameter name

Description

expression

The OGNL expression to be evaluated (to a boolean value).

Struts Expression Validator XML Examples:

Some OGNL comparison operators we can use are:

 

2. Struts @ExpressionValidator Annotation

Usage: Put the @ExpressionValidatorannotation before the action method in the following form:

@ExpressionValidator (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.

shortCircuit

No

false

Whether this validator is short circuit.

expression

Yes

 

The OGNL expression to be evaluated (to a boolean value).



Struts @ExpressionValidator 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.