Friday, 10 July 2015

Struts2 Date Validation Example

The date validator checks that given date is witin the specified range.

Parameters of date validator

There is 3 parameters defined for date validator.
ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.
minspecifies the minimum range. It is ignored bydefault.
maxspecifies the maximum range. It is ignored bydefault.

Example of date validator

  1. <validators>  
  2.   <!-- Plain Validator syntax -->  
  3.          <validator type="date">  
  4.       <param name="fieldName">dob</param>  
  5.           <param name="min">01/01/1980</param>  
  6.           <param name="max">01/01/2010</param>  
  7.           <message>Date of Birth must be within ${min} and ${max}</message>  
  8.     </validator>      
  9. </validators>  
  1. <validators>  
  2.    <!-- Field Validator Syntax -->  
  3.       <field name="dob">  
  4.         <field-validator type="date">  
  5.             <param name="min">01/01/1980</param>  
  6.               <param name="max">01/01/2010</param>  
  7.               <message>Date of Birth must be within ${min} and ${max}</message>  
  8.         </field>  
  9.       </field>  
  10.   
  11. </validators>  

Full example of date validator

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. <s:form action="register">  
  4. <s:textfield name="dob" label="Date of Birth"></s:textfield>  
  5. <s:submit value="register"></s:submit>  
  6. </s:form>  

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
  1. package com.javatpoint;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class Register extends ActionSupport{  
  8. private Date dob;  
  9.   
  10.   
  11. public Date getDob() {  
  12.     return dob;  
  13. }  
  14.   
  15.   
  16. public void setDob(Date dob) {  
  17.     this.dob = dob;  
  18. }  
  19.   
  20.   
  21. public String execute(){  
  22.     return "success";  
  23. }  
  24.   
  25. }  

3) Create the validation file

Here, we are using bundled validators to perform the validation.
Register-validation.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <!DOCTYPE validators PUBLIC   
  4.         "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
  5.         "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  6.   
  7.         <validators>  
  8.           
  9.         <field name="dob">  
  10.         <field-validator type="date">  
  11.         <param name="min">01/01/1950</param>  
  12.         <param name="max">01/01/2010</param>  
  13.           
  14.         <message>Date of Birth must be between ${min} to ${max}</message>  
  15.         </field-validator>  
  16.         </field>  
  17.           
  18.           
  19.         </validators>  

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5. <package name="default" extends="struts-default">  
  6. <action name="register" class="com.javatpoint.Register">  
  7. <result name="input">index.jsp</result>  
  8. <result>welcome.jsp</result>  
  9. </action>  
  10. </package>  
  11.   
  12. </struts>      
  13.      
  14.    

5) Create view component

It is the simple jsp file displaying the information of the user.
welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Date of Birth:<s:property value="dob"/>