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

Compare with Current View Page History

« Previous Version 111 Next »

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.

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-side Validation
  3. AJAX Validation
  4. Using Field Validators
  5. Using Non Field Validators
  6. Using Visitor Field Validator
  7. How do we repopulate controls when validation fails (FAQ entry)

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

The following list shows the default validators included in the framework and is an example of the syntax used to declare our own validators.

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 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:

<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"/>

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

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