Struts2 Date Validation Example

The date validator watches that given date is witin the predetermined range.

Parameters of date validator

There is 3 parameters characterized for date validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.
minspecifies the base range. It is disregarded bydefault.
maxspecifies the most extreme range. It is overlooked bydefault.

Example of date validator

<validators>  
  •   <!-- Plain Validator syntax -->  
  •          <validator type="date">  
  •       <param name="fieldName">Jhonson</param>  
  •           <param name="min">01/01/1980</param>  
  •           <param name="max">01/01/2010</param>  
  •           <message>Date of Birth must be within ${min} and ${max}</message>  
  •     </validator>      
  • </validators>  
    1. <validators>  
    2.    <!-- Field Validator Syntax -->  
    3.       <field name="Jhonson">  
    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="Jhonson" 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.javacodegeeks;  
      2.   
      3. import java.util.Date;  
      4.   
      5. import com.opensymphony.xwork2.ActionSupport;  
      6.   
      7. public class Register extends ActionSupport{  
      8. private Date Jhonson;  
      9.   
      10.   
      11. public Date getJhonson() {  
      12.     return Jhonson;  
      13. }  
      14.   
      15.   
      16. public void setJhonson(Date Jhonson) {  
      17.     this.Jhonson = Jhonson;  
      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="Jhonson">  
      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.javacodegeeks.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="Jhonson"/>  





    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition