The Date validator in the Struts 2 Framework checks
whether the supplied date lies within a specific range or not. If the value
supplied does not lie in the specified range, it generates an error message.
The error message is supplied between the <message> </message> tag. The following example demonstrates how to use the date validator to check the input range.
[ NOTE: If date converter is not specified then XWorkBasicConverter
will kick in to do the date conversion, which by default uses the
format (using a programmatically specified locale else falling back to the
system default locale).]
Follow the steps to develop the date range validator :
Step 1: Create the struts.xml file and add the following xml snippet in the struts.xml file.<?xml version="1.0" encoding="UTF-8" ?>
Step 2 : Create the input jsp form i.e.
dateInputForm.jsp<%@ taglib prefix="s" uri="/struts-tags" %>
Step 3 : Create an Action class.
Step 4 : Create a Date validator with in an xml file:
The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionAliasName>-validation.xml.
Date validator: This Field Validator checks if the date supplied is within a specific range. If it is valid and lies within the specified range then you are sent to the dateSuccess.jsp page. Otherwise, it displays the message given in the xml file (e.g. Joining date must be supplied between 01/01/1990 and 01/01/2000.). The date validator takes the following parameters:
DateVaLidationAction-validation.xml<?xml version="1.0" encoding="UTF-8"?>
When any date that has to be passed in the
text field by the user is correct then dateSuccess.jsp
page is displayed as shown below:
"Employee Joining Date: 12/11/1998 " message.
dateSuccess.jsp<%@page language="java" %>
When this application executes, the following page is recieved:
If you enter the wrong data or text then you get:
If you enter the date below the range:
If you enter the date out of the range, then you get the output as:
If you enter the date lying between the specified range:
Then you get the output message as shown:
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.