Struts Expression Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
- XML: using type=”expression” attribute in the <validator> element.
- Annotation: using @ExpressionValidator annotation type to annotate action method.
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:
- Suppose that we want to check if two integer fields, number1and number2 to ensure that the number1is always less than the number2:
<validator type="expression"> <param name="expression">number1 lt number2</param> <message>The number #1 must be less than the number #2</message> </validator>
- In the following example, the expression validator ensures that the end date is always after the start date:
<validator type="expression"> <param name="expression">startDate lt endDate</param> <message>The start date must be before the end date</message> </validator>
- Suppose that we want to check if two integer fields, number1and number2 to ensure that the number1is always less than the number2:
- lt: less than
- lte: less than or equal
- gt: greater than
- gte: greater than or equal
- eq: equal
- neq: not equal
- etc
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:
- Annotating the action execute()method:
@ExpressionValidator( expression = "number1 lt number2", message = "The number #1 must be less than the number #2" ) public String execute() { return SUCCESS; } - Specifying i18n key for the message:
@ExpressionValidator( expression = "number1 lt number2", message = "the default message if the i18n key not found", key = "form.validation.numbers" ) public String execute() { return SUCCESS; }
- Annotating the action execute()method:
- 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 Field 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
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments