Struts Action 2 relies on a validation framework provided by [XWork] to enable the application of input validation rules to your Actions before they are executed. This section only provides the bare minimum to get you started and focuses on our extension of the XWork validators to support client-side validation.
There is also an option for Client Side (Javascript and/or AJAX) based validation, please see Client Side Validation for more information.
Using Annotations
Annotations can be used as an alternative to XML for validation.
Examples
In all examples given here, the validation message displayed is given in plain English - to internationalize the message, put the string in a properties file and use a property key instead. It will be looked up by the framework.
- Basic Validation
- Client Validation
- AJAX Validation
- Using Field Validators
- Using Non Field Validators
- Using Visitor Field Validator
Bundled Validators
Note
When using a Field Validator, Field Validator Syntax is ALWAYS preferable than using the Plain Validator Syntax as it facilitates grouping of field-validators according to fields. This is very handy especially if a field needs to have many field-validators which is almost always the case. Examples: validatortypes
- conversion validator
- date validator
- double validator
- email validator
- expression validator
- fieldexpression validator
- int validator
- regex validator
- required validator
- requiredstring validator
- stringlength validator
- url validator
- visitor validator
Registering Validators
This list declares all the validators that comes with the framework.
Note
Warning
This does not appear to be true as of Struts 2.0.8, the validators.xml
containing the custom validation does not need to contain a copy of the default validators. (See the Struts 2.0.8 release notes.)
Turning on Validation
Note: The default validationWorkflowStack already includes this.
You can turn off validation for a specific method by using the @SkipValidation annotation above your action method.