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, specified by the 'key' attribute. It will be looked up by the framework (see Localization).
- 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.
- 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. It is provided as an example of the syntax, you should not need to declare any of these validators unless you are replacing the defaults.
Struts 2.0.7 and Prior
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 defaultStack
already has validation turned on. When creating your own interceptor-stack be sure to include the validation
interceptor. From struts-default.xml
:
<interceptor-stack name="defaultStack"> ... <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
Beginning with version 2.0.4 Struts provides an extension to XWork's com.opensymphony.xwork2.validator.ValidationInterceptor
interceptor.
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
Using this validator you can turn off validation for a specific method by using the @org.apache.struts2.interceptor.validation.SkipValidation
annotation on your 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.