The field expression validator validates a field using an OGNL expression. It is very similar to the expression validator, except that it displays the error message next to the field instead of on top of the form.

This validator can be used in either of the following forms:

 

1. Struts Field Expression Validator XML

Usage:

Parameters:

Parameter name

Description

expression

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

 

Struts Field Expression Validator XML Examples:

Suppose that we have a form with three fields: number #1, number #2 and sum of these two numbers:

field expression form test

We want that the user always enters the sum correctly. The following examples illustrate that purpose:

Here’s the screenshot when the user enters a wrong number for the sum field:

field expression validation error

In the above examples, we use the expression:

sum eq (number1 + number2)

That’s equivalent to:

sum = (number1 + number2)

Some OGNL comparison operators we can use are:

 

2. Struts @FieldExpressionValidator Annotation

Usage: Put the @FieldExpressionValidator annotation before the field’s getter/setter method or action method in the following form:

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

expression

Yes

 

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

 

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