Hi all,
I've just started exploring the use of Struts DynaValidatorForms. I've
tried to set-up a very simple login form with a username and password
fields, where both are required. Here's the relevant details:

1. struts-config.xml
The application message resource bundle has been configured with the
required validation messages for the validation framework, and the
validator plugin is also declared.
I declared a form bean as follows:
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorForm">form-bean
name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="password" type="java.lang.String" />
<form-property name="userName" type="java.lang.String" />
<form-property name="userName" type="java.lang.String" />
</form-bean>
Then I declared a action tied to the form bean as follows:
<action
      attribute="loginForm"
      input="/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="project.struts.actions.LoginAction"
      validate="true">
      <forward name="success" path="/index.jsp" redirect="true"/>
      <forward name="failure" path="/login.jsp" redirect="true"/>
</action>

2. login.jsp
This is the JSP with the form. It uses both client and server-side
validation.The code is as follows:
<body>
<html:errors/>
    <html:form action="login" method="post" focus="userName"
onsubmit="return validateLoginForm(this);">
      <table border="0">
        <tr>
          <td>Login:</td>
          <td><html:text property="userName" /></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><html:password property="password" /></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><html:submit value="Login"/></td>
        </tr>
      </table>
    </html:form>
    <html:javascript formName="loginForm"/>
</body>

3. validation.xml
I declared both the "userName" and the "password" fields to be
"required", as follows:
<formset>
<form name="loginForm">
    <field property="userName" depends="required">
        <arg0 name="required" key="loginForm.userName"/>
    </field>
    <field property="password" depends="required">
        <arg0 name="required" key="loginForm.password"/>
    </field>
</form>
</formset>

4. Action class, LoginAction.java
Here's the execute mehod implementation. Basically, it rejects the
login when the userName and password are not both "test".
public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse 
response) {
    DynaValidatorForm loginForm = (DynaValidatorForm) form;
    String userName = loginForm.getString("userName");
    String password = loginForm.getString("password");
    if ((userName == null) || (!userName.equals("test"))) {
        return mapping.findForward("failure");
    } else if ((password == null) || (!password.equals("test"))) {
        return mapping.findForward("failure");
    } else {
        return mapping.findForward("success");
    }
}

I then tried to test the above by deploying the application, hitting
login.jsp and attempting to login with invalid userName and password.
The Javascript validation works fine, and the messages display
correctly => The message resource is configured correctly.

However when I turned off Javascript on my bowser to test the
server-side validation. The validation did not take place at all! If I
supplied a blank userName and/or password, it simply performs the
logic in LoginAction and sends me back to login.jsp, without any error
messages!

Anyone has any ideas what I might have missed out here?

Thanks in advance for any suggestions!

Weng Kong Lee

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to