Struts 2 validation is configured via XML or annotations. Manual validation in the action is also possible, and may be combined with XML and annotation-driven validation.
Validation also depends on both the validation
and workflow
interceptors (both are included in the default interceptor stack). The validation
interceptor does the validation itself and creates a list of field-specific errors. The workflow
interceptor checks for the presence of validation errors: if any are found, it returns the "input" result (by default), taking the user back to the form which contained the validation errors.
If we're using the default settings and our action doesn't have an "input" result defined and there are validation (or, incidentally, type conversion) errors, we'll get an error message back telling us there's no "input" result defined for the action.
CONTENTS
Using Annotations
Annotations can be used as an alternative to XML for validation.
Bean Validation
With struts 2.5 comes the Bean Validation Plugin. That is an alternative to the classic struts validation described here. See the Plugin Page for details.
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, specified by the 'key' attribute. It will be looked up by the framework (see Localization).
- Basic Validation
- Client-side Validation
- AJAX Validation
- Using Field Validators
- Using Non Field Validators
- Using Visitor Field Validator
- How do we repopulate controls when validation fails (FAQ entry)
Bundled Validators
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.
- conversion validator
- date validator
- double validator
- email validator
- expression validator
- fieldexpression validator
- int validator
- regex validator
- required validator
- requiredstring validator
- short validator
- stringlength validator
- url validator
- visitor validator
- conditionalvisitor validator
Registering Validators
The validators.xml
used to reference a DTD hosted by Opensymphony, the original location of the XWork project. Since the the move to Apache Struts, DTDs were changed. Please ensure in your projects to include the DTD header as described in the examples found here
The validators.xml
containing custom validators needs to contain a copy of the default validators. No DTD was used in validators.xml. See: http://struts.apache.org/2.x/docs/release-notes-208.html#ReleaseNotes2.0.8-MigrationfrompreviousReleases
Turning on Validation
The default interceptor stack, "defaultStack", already has validation turned on. When creating your own interceptor-stack be sure to include both the validation
and workflow
interceptors. From struts-default.xml
:
Beginning with version 2.0.4 Struts provides an extension to XWork's com.opensymphony.xwork2.validator.ValidationInterceptor
interceptor.
This interceptor allows us to turn off validation for a specific method by using the @org.apache.struts2.interceptor.validation.SkipValidation
annotation on the action method.
Validator Scopes
Notes
Defining Validation Rules
In this context, "Action Alias" refers to the action name as given in the Struts configuration. Often, the name attribute matches the method name, but they may also differ.
Localizing and Parameterizing Messages
Validator Flavor
Non-Field Validator Vs Field-Validator validatortypes
There are two ways you can define validators in your -validation.xml file:
- <validator>
- <field-validator>
Keep the following in mind when using either syntax:
Non-Field-Validator: The <validator> element allows you to declare both types of validators (either a plain Validator a field-specific FieldValidator).
field-validator: The <field-validator> elements are basically the same as the <validator> elements except that they inherit the fieldName attribute from the enclosing <field> element. FieldValidators defined within a <field-validator> element will have their fieldName automatically filled with the value of the parent <field> element's fieldName attribute. The reason for this structure is to conveniently group the validators for a particular field under one element, otherwise the fieldName attribute would have to be repeated, over and over, for each individual <validator>.
It is always better to defined field-validator inside a <field> tag instead of using a <validator> tag and supplying fieldName as its param as the xml code itself is clearer (grouping of field is clearer)
Note that you should only use FieldValidators (not plain Validators) within a block. A plain Validator inside a <field> will not be allowed and would generate error when parsing the xml, as it is not allowed in the defined dtd (xwork-validator-1.0.2.dtd)
Declaring a FieldValidator using the <field-validator> syntax:
The choice is yours. It's perfectly legal to only use elements without the elements and set the fieldName attribute for each of them. The following are effectively equal:
Short-Circuiting Validator
How Validators of an Action are Found
Writing custom validators
If you want to write custom validator use on of these classes as a starting point:
- com.opensymphony.xwork2.validator.validators.ValidatorSupport
- com.opensymphony.xwork2.validator.validators.FieldValidatorSupport
- com.opensymphony.xwork2.validator.validators.RangeValidatorSupport
- com.opensymphony.xwork2.validator.validators.RepopulateConversionErrorFieldValidatorSupport
2 Comments
Matthieu Chase Heimer
There are some errors from XWork snippets. See: http://jira.opensymphony.com/browse/XW-633 http://jira.opensymphony.com/browse/XW-634
Once these changes have been committed re-export this page to html.
Rob Tanzola
The DTDs moved to struts.apache.org with the retirement of the OpenSymphony projects, so the examples of the validation definition could be pointed to http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd and validation rules examples could be updated to point to the http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd.