You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by de...@struts.apache.org on 2004/05/25 18:30:49 UTC

[Apache Struts Wiki] New: StrutsBeginnerValidatorFramework

   Date: 2004-05-25T09:30:49
   Editor: 165.21.154.75 <>
   Wiki: Apache Struts Wiki
   Page: StrutsBeginnerValidatorFramework
   URL: http://wiki.apache.org/struts/StrutsBeginnerValidatorFramework

     

New Page:

##language:en
== Template for Help Pages ==

Disclaimer :

I am just another struts beginner who would like to contribute something back to the community. Any comments on the article are welcome. Please do the necessary amendment if I do any mistakes here :D

Hope this article can benefit the entire community.


Struts validator framework is neat way of validating you UI input. Some of the most common UI input validation includes, checking for mandatory fields, email validation, credit card validation, etc. 


Here are some necessary step before we can begin using the struts validation framework

  1. Create an action form class that extends oeg.apache.struts.validator.ValidatorForm.
  2. Create an action class.
  3. Set up the resource bundle for the application in struts config file.
  4. Create a jsp page that serves as an input.
  5. Setup struts-config.xml
  6. Copy the validator-rules.xml and validator.xml into WEB-INF.
  7. Add the form to the form set in the validator.xml
  

''' Step 1. Create an action Form class '''

{{{ 

public class UserNameForm extends ValidatorForm {

    public String userName;
    
    public void setUserName(String userName)
    {
          this.userName = userName;
    }

    public String getUserName ()
    {
          return this.userName;
    }
}}}}

This form will contain one field called user name field, that is mandatory.

''' Step 2. Create a action class '''
{{{

public class UserNameAction extends Action
{
    public ActionForward viewTopicList(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response){

            UserNameForm userNameForm =  (UserNameForm) form;
            return mapping.findForward("success");
    }
 
}

}}}

''' Step 3. Set up the reource bundle '''

{{{
<message-resources parameter="ApplicationResources"/>
}}}

Specify a resource bundle key-value pair

{{{
userNameForm.userName= User Name
}}}

''' Step 4. Create a JSP as an input '''

{{{

 ....
 <logic:messagesPresent>
 <%-- Print out the error message if the user forgot to key in their user name --%>
 <html:messages id="error"/>
   <%= error %>
  </html:messages>
 </log:messagesPresent>

 <html:form action="inputSubmit">
 <html:text property="userName"/>
 <html:submt value="submit"/>
 </html:form>
 ...
 
}}}

''' Step 5. Map the jsp page with the action form and the action class inside struts-config.xml. '''

{{{

  <!-- configure the form -->
  <form-beans>
    <form-bean
     name="userNameForm"
     type="UserNameForm"/>
  </form-beans>
  <!-- configure the action mapping -->
  <action
   path="/inputSubmit"
   type="UserNameAction"
   name="userNameForm"
   scope="request"
   validate="true"
   input="userNameInput.jsp">
  <!-- confgure the plug in -->
  <plug-in 
   className="org.apache.strus.validator.ValidatorPlugIn">
   <set-property
    property="pathnames"
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
  </plug-in>

}}}

''' Step 7. Add the form to the form set in the validator.xml''' 

{{{
<form-validation>
<formset>
   <form name="userNameForm">
      <field property="userName"
             depends="required"/>
   </form>
</formset>
</form-validation>
}}}

the depends attribute specify what validation rules that we should apply to the userName field on the input.jsp. Here we specify required, which mean eveytime the user forgot to key in something in the userName field. The validator framework will make a validation error.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org