STRUTS VALIDATION FRAMEWORK

 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.

Sincerely,
Irfandhy Franciscus

There is more info on Validator over at the jakarta-commons/validator wiki http://wiki.apache.org/jakarta-commons/ValidatorSetup

Some of the most common UI input validation includes, checking for mandatory fields, email validation, credit card validation, etc. The Validator framework is a great way of validating your UI input.

The core of the Validator framework comes from jakarta-commons where, much like the other components, it is maintained as a separate, independent project that does not require Struts. The Validator Plugin allows us to take advantage of that library in a Struts-familiar way.

There are 2 different ways to use the validator in Struts. Form-based validation and Action-based validation. The differences are explained in detail in the users guide, but we'll give a quick summary here.

  • Form-based validation is validation that gets applied to a particular form name. You can have different validations on the same form, if you specify a different name.
  • Action-based validation is validation that gets applied for a specific path (such as "/createCustomer.do").

Here are some necessary step before we can begin using the struts validation framework with "form-based" validation (as described above).

  1. Create an action form class that extends org.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 resource bundle

<message-resources parameter="ApplicationResources"/>

Specify a resource bundle key-value pair

userNameForm.userName= User Name

Step 4. Create a JSP as an input

We name this JSP as input.jsp

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

 <html:form action="inputSubmit" onsubmit="return validateUserNameForm(this);">
 <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-mappings>
   <action
    path="/inputSubmit"
    type="UserNameAction"
    name="userNameForm"
    scope="request"
    validate="true"
    input="input.jsp"/>
  </action-mappings>
  <!-- confgure the plug in -->
  <plug-in 
   className="org.apache.struts.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 everytime the user forgot to key in something in the userName field, the validator framework will alert the user that they have not key in any value in the field.

  • No labels