Struts 2 validation framework provides many generic built-in validators to perform various validations like email validation, required validation, stringlength validation etc. These validations are also known as XML based validation. Built-in validators are placed in an xml file. The name of the xml file should be like actionClassName-validation.xml.
Note: No need to specify any validate() method in the action class.
Types of validators in struts 2.
- Field Validators.
- Non-field validators.
1. Field validators:
These validators act or perform validations on the single field i.e. field validators are not generic they are specific to the single field. More than one validator can be applied to one field.
Syntax:
<validators> <field name="fieldName"> <field-validator type="validatorName"> <message>message string</message> </field-validator> </field> </validators> |
2. Non-field validators:
These validators are the action level validators. Single validator can be applied to multiple fields.
Note: In this approach only one validator can be applied to a single field.
Syntax:
<validators> <validator type=" validatorName "> <param name="fieldName">username</param> <message> message string </message> </validator> </validators> |
Next Topic: Struts 2 required validator with example.
Previous Topic: Struts 2 validation framework with example.