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> 
 

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> 
 

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!)"
      ) 
 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

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.



Add comment