Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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

Code Block
formatXML
titleLoginLogon-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 LoginLogon-validation.xml next to the LoginLogon.java class, and paste in the code.

...

(minus) <li><a href="<s:url action="LoginLogon"/>">Sign On</a></li>
(plus) <li><a href="<s:url action="LoginLogon_input"/>">Sign On</a></li>

struts.xml

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

How the Code Works

To open the Login Logon form, the Welcome page refers to LoginLogon_input.

  • The framework matches this reference with the LoginLogon_* action mapping.
  • The "method={1}" attribute is replaced with "method=input".
  • The framework invokes the input method on the Login Logon 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 "LoginLogon.jsp" as the response, without any validation messages.

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

  • The framework checks for a validation for the target Action class, Login Logon.
  • 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.

...