You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 107 Next »

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).

  1. Basic Validation
  2. Client Validation
  3. AJAX Validation
  4. Using Field Validators
  5. Using Non Field Validators
  6. 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.

  1. conversion validator
  2. date validator
  3. double validator
  4. email validator
  5. expression validator
  6. fieldexpression validator
  7. int validator
  8. regex validator
  9. required validator
  10. requiredstring validator
  11. stringlength validator
  12. url validator
  13. visitor validator

Registering Validators

Error formatting macro: snippet: java.lang.NullPointerException

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.

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Notes

Error formatting macro: snippet: java.lang.NullPointerException

Defining Validation Rules

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Validator Flavor

Error formatting macro: snippet: java.lang.NullPointerException

Non-Field Validator Vs Field-Validator

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Short-Circuiting Validator

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

How Validators of an Action are Found

Error formatting macro: snippet: java.lang.NullPointerException

Resources

WebWork Validation

Next: Localization

  • No labels