Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Table of Contents
minLevel2
outlinetrue
stylenone

Using Annotations

Annotations can be used as an alternative to XML for validation.

...

Wiki Markup
{snippet:lang=xml|url=resources.com.opensymphony.xwork2.struts2/xwork-core/src/main/resources/comopensymphony/xwork2/validator/validators/default.xml}

...

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:

Code Block
langxml

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

Code Block
langxml

<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

...

Non-Field-Validator: The <validator> element allows you to declare both types of validators (either a plain Validator a field-specific FieldValidator).

Code Block
xml
xml

<validator type="expression> 
    <param name="expression">foo gt bar</param> 
    <message>foo must be great than bar.</message> 
</validator> 
Code Block
xml
xml

<validator type="required"> 
    <param name="fieldName">bar</param> 
    <message>You must enter a value for bar.</message> 
</validator> 

...

Declaring a FieldValidator using the <field-validator> syntax:

Code Block
xml
xml

<field name="email_address"> 
    <field-validator type="required"> 
        <message>You cannot leave the email address field empty.</message> 
    </field-validator> 
    <field-validator type="email"> 
        <message>The email address you entered is not valid.</message> 
    </field-validator> 
</field> 

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:

Code Block
xml
xml

<field name="email_address"> 
    <field-validator type="required"> 
        <message>You cannot leave the email address field empty.</message> 
    </field-validator> 
    <field-validator type="email"> 
        <message>The email address you entered is not valid.</message> 
    </field-validator> 
</field> 
 
<validator type="required"> 
    <param name="fieldName">email_address</param> 
    <message>You cannot leave the email address field empty.</message> 
</validator> 
<validator type="email"> 
    <param name="fieldName">email_address</param> 
    <message>The email address you entered is not valid.</message> 
</validator> 

...

Resources

WebWork Validation

Next: Localization