Struts Field Expression Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
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:
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:
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:
- 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 Required Field Validator Example
- Struts Expression Validator Example
- Struts URL 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