You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Nathan Bubna <na...@esha.com> on 2003/01/14 16:11:48 UTC

Re: Validator framework with Velocity/Struts [LONG]

Eric said:
> Has anyone managed to get the Validator framework working with
> Velocity/Struts?  If so, I would be interested to know how.

yeah, there's actually not a whole lot to it.  here's a snippet of what i
generally do for a login form using Struts 1.1-b3:

[struts-config.xml]
...
  <form-beans>
    <form-bean name="loginForm"
               dynamic="true"
               type="org.apache.struts.validator.DynaValidatorForm">
      <form-property name="username"  type="java.lang.String"/>
      <form-property name="password"  type="java.lang.String"/>
    </form-bean>
  </form-beans>
...
  <action-mappings>
    <action path="/login"
            type="com.esha.web.action.LoginAction"
            name="loginForm"
            scope="request"
            validate="true"
            input="/Login.vm">
    </action>
  </action-mappings>
...
  <message-resources parameter="ApplicationResources"/>
...
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
                  value="/WEB-INF/validator-stuff.xml"/>
  </plug-in>


[validator-stuff.xml]
<form-validation>
  <global>
    <validator name="required"
               classname="org.apache.struts.util.StrutsValidator"
               method="validateRequired"
               methodParams="java.lang.Object,
                             org.apache.commons.validator.ValidatorAction,
                             org.apache.commons.validator.Field,
                             org.apache.struts.action.ActionErrors,
                             javax.servlet.http.HttpServletRequest"
               msg="errors.required"/>
  </global>
  <!-- it's actually better to put these in separate files and list both
         filenames in the pathnames attribute of your ValidatorPlugin's
        set-property, but this works too.  -->
  <formset>
    <form name="loginForm">
      <field property="username" depends="required">
        <arg0 key="Username" resource="false"/>
      </field>
      <field property="password" depends="required">
        <arg0 key="Password" resource="false"/>
      </field>
    </form>
  </formset>
</form-validation>


[ApplicationResources.properties]
...
errors.required={0} is required.
...


[Login.vm]
...
<form action="$link.setAction('login')" method="post" name="login">
<table border="0" cellspacing="1" cellpadding="0">
#foreach( $e in $errors.username )
  <tr>
    <td colspan="2" class="error">$!e</td>
  </tr>
#end
  <tr>
    <td>Username:</td>
    <td><input type="text" name="username" size="25"
value="$!form.bean.username"></td>
  </tr>
#foreach( $e in $errors.password )
  <tr>
    <td colspan="2" class="error">$!e</td>
  </tr>
#end
  <tr>
    <td>Password:</td>
    <td><input type="password" name="password" size="25"></td>
  </tr>
  <tr>
    <td colspan="2">
      <input type="submit" class="button" value="Login">
    </td>
  </tr>
</table>
</form>
...


[toolbox.xml]
...
  <tool>
    <key>link</key>
    <scope>request</scope>
    <class>org.apache.velocity.tools.struts.LinkTool</class>
  </tool>
  <tool>
    <key>form</key>
    <scope>request</scope>
    <class>org.apache.velocity.tools.struts.FormTool</class>
  </tool>
  <tool>
    <key>errors</key>
    <scope>request</scope>
    <class>org.apache.velocity.tools.struts.ErrorsTool</class>
  </tool>
...


That's probably more detail than you need, but better more than less i
think.  In any case, using the struts validator stuff is pretty easy once
you figure it out.  i've toyed with the idea of trying to come up with a
solution that requires less coordination between all these different files
(ala Turbine's Intake), but i haven't had the time to look into that.

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>