Versions Compared

Key

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

...

Like the mappings, validation is described through an XML document. The document is named after the Action being validated with a "-validation" suffix. Since we would like to validate the Login Action class, our document is named Logon-validation.xml.

Code Block
formatxmlXML
titleLogin-validation.xml
borderStylesolid
<!DOCTYPE validators PUBLIC 
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <message>Username is required</message>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="requiredstring">
            <message>Password is required</message>
        </field-validator>
    </field>
</validators>

Create a file named Login-validation.xml next to the Login.java class, and paste in the code.

The first time a page displays, we wouldn't want the validation to fire. We should have a chance to enter the input before being told it's incorrect. One way to bypass validation is to refer to a special "input" method provided by the base ActionSupport class. To do that, we need to edit the Welcome page and the Logon mapping.

To indicate the changes, we'll use (minus) to indicate a line we are removing, and (plus) to indicated a line we are adding.

Welcome.jsp

(minus) <li><a href="<s:url action="Login"/>">Sign On</a></li>
(plus) <li><a href="<s:url action="Login!input"/>">Sign On</a></li>

struts.xml

(minus) <action name="Login" class="tutorial.Login">
(plus) <action name="Login!*" method="{1}" class="tutorial.Login">

How the Code Works

When To open the Login form is submitted, the , the Welcome page refers to Login!input.

  • The framework matches this reference with the Login!* action mapping.
  • The "method={1}" attribute is replaced with "method=input".
  • The framework invokes the input method on the Login Action class.
  • Since "input" is on a special list of methods that bypass validation, the validation framework is not invoked.
  • The default input method returns the result code "input".
  • The framework renders "Login.jsp" as the response, without any validation messages.

To submit the Login form, the Login pages refers to Login.

  • The framework checks for a validation for the target Action class, Login.
  • Finding a Logon-validation.xml file, the framework creates a validation object for the class, based on the XML document.
    • Essentially, the validation is a set of Validator objects.
  • The Validators are applied to the incoming properties.
  • If a Validator fails,

...

  • its message is added to an internal queue.
  • When all the Validators have fired, if the framework sees that errors have been posted,

...

  • it seeks the "input" result

...

  • , without invoking the Action class.
  • Otherwise, the default Action method fires. Since the input has already been validated, the "success" result code is returned.

What to Remember

...

The framework provides a validation framework. An A set of validators can be associated with an input field. If validation fails, the framework can return to the input page and display the error messages. To bypass validation, a special "input" Action method can be invoked, instead of the default "execute" method.

(lightbulb) For more, see Validation in the Core Developers Guide.

...