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:

  • XML: using type=”fieldexpression” attribute in the <validator> or <field-validator> elements.
  • Annotation: using @FieldExpressionValidator annotation type to annotate the field’s getter/setter method, or action method.
 

1. Struts Field Expression Validator XML

Usage:

  • Field-validator syntax:
    <field name="myField">
    	<field-validator type="fieldexpression">
    	    <param name="expression">OGNL expression</param>
    	    <message>validation error message</message>
    	</field-validator>
    </field>
     

  • Plain-validator syntax:
    <validator type="fieldexpression">
    	<param name="fieldName">myField</param>
    	<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 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:

  • Using field-validator example:
    <field name="sum">
    	<field-validator type="fieldexpression">
    		<param name="expression">sum eq (number1 + number2)</param>
    		<message>The sum you entered is incorrect!</message>
    	</field-validator>
    </field>


     

  • Using plain-validator example:
    <validator type="fieldexpression">
    	<param name="fieldName">sum</param>
    	<param name="expression">sum eq (number1 + number2)</param>
    	<message>The sum you entered is incorrect!</message>
    </validator>
     

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:

  • lt: less than
  • lte: less than or equal
  • gt: greater than
  • gte: greater than or equal
  • eq: equal
  • neq: not equal
  • etc
 

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:

  • Annotating the field’s setter/getter method:
    @FieldExpressionValidator(
    	expression = "sum eq (number1 + number2)",
    	message = "The sum you entered is incorrect!"
    )
     

  • Annotating the action execute() method:
    @FieldExpressionValidator(
    	fieldName = "sum",			
    	expression = "sum eq (number1 + number2)",
    	message = "The sum you entered is incorrect!"
    )
    public String execute() {
    	return SUCCESS;
    }
     

  • Specifying i18n key for the message:
    @FieldExpressionValidator(
    	fieldName = "sum",			
    	expression = "sum eq (number1 + number2)",
    	key = "form.validation.sum",
    	message = "(default message if the i18n key not found)" 
    )
 

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.



Add comment