Struts Conversion Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
The conversion validator is a Struts field validator that checks if a type conversion error occurs for a field. For example, if the user inputs the value “one” for an integer field, a conversion error will occur, and an error message will be displayed next to the field. This validator can be used in either of the following forms:
- XML: using type=”conversion” attribute in <validator> or <field-validator> elements.
- Annotation: using @ConversionErrorFieldValidator annotation type to annotate getter/setter method of the field or action method (plain-validator).
1. Struts Conversion Validator XML
Usage:
- Field-validator syntax:
<field name="fieldName"> <field-validator type="conversion"> <message>conversion error message</message> </field-validator> </field>
- Plain-validator syntax:
<validator type="conversion"> <param name="fieldName">myField</param> <message>conversion error message</message> </validator>
- Field-validator syntax:
Parameters:
Parameter name | Description |
fieldName | Name of the field to validate. Required if using plain validator syntax. |
Struts Conversion Validator XML Examples:
- Field-validator example:
<field name="myAge"> <field-validator type="conversion"> <message>Please enter a number for your age!</message> </field-validator> </field>
- Plain-validator example:
<validator type="conversion"> <param name="fieldName">myAge</param> <message>Age must be a number!</message> </validator>
- If you want to re-populate the user-entered value when the conversion error occurs, use the parameter repopulateFieldas follows:
<field name="myAge"> <field-validator type="conversion"> <param name="repopulateField">true</param> <message>Please enter a number for your age!</message> </field-validator> </field>
- Field-validator example:
2. Struts @ConversionErrorFieldValidator Annotation
Usage: Put the @ConversionErrorFieldValidatorannotation before the field’s setter/getter method or action method (in case of using plain-validator) in the following form:
@ConversionErrorFieldValidator (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. |
type | No | ValidatorType.FIELD | type of the validator: field-validator (FIELD) or plain-validator (SIMPLE). |
Struts @ConversionErrorFieldValidator Annotation Examples:
- Basic field-validator (annotating field’s getter method):
@ConversionErrorFieldValidator( message = "Age must be a number (integer wanted!)" ) public int getMyAge() { return myAge; }
- Specifying i18n key for the message (annotating field’s setter method):
@ConversionErrorFieldValidator( key = "form.validation.age", message = "Age must be a number (integer wanted!)" // default message ) public void setMyAge(int myAge) { this.myAge = myAge; }
- Plain-validator (annotating the action method):
@ConversionErrorFieldValidator( type = ValidatorType.SIMPLE, fieldName = "myAge", message = "Age must be a number (integer wanted!)" )
- Basic field-validator (annotating field’s getter method):
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 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
Comments