Struts 2 Date Validator

In the struts web application, a date validator is used to check whether the entered date is within the specified range. Three parameters defined with date validator are:

  • fieldName: It is used to specify the name of the field which is to be validated.
  • min: This parameter is used to describe the minimum date allowed.
  • max: This parameter is used to describe the maximum date allowed.

Struts Bundled Validators - date Example:

To get input from the user create the index.jsp file:

The index.jsp page takes the input from the user. The user will enter the email, password and the Date of Birth. Then after clicking on the Register button, it will redirect to the next resource. It contains three textfield and one button on the form with the link to LoginAction class

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>  
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<s:form action="login">
<s:textfield name="email" label="Enter the email"></s:textfield>
<s:textfield name="pwd" label="Enter the password"></s:textfield>
<s:textfield name="dob" label="Enter the dob"></s:textfield>


<s:submit value="Register"></s:submit>
</s:form>
</body>
</html>

Create welcome.jsp file to denote success:

The welcome.jsp page displays Welcome to the world of the Technology when execute method return success string.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
Welcome to the world of the Technology. 
</body>
</html>

Create error.jsp file to denote failure:

The error.jsp page displays Sorry wrong Email, Password or DOB!!! on failure. When the execute() method returns an error, this page executes.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
</head>
<body>
Sorry wrong Email, Password or DOB !!!
</body>
</html>

Create the action class LoginAction.java:

The Action class LoginAction.java contains three fields email, dob, and password with their getter and setters. It contains the execute method. On success, it will go to the welcome.jsp page otherwise on error it will redirect to error.jsp.

import java.util.Date;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport
{
private String email;
private String pwd;
private Date dob;

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}

@Override
public String execute() throws Exception {
return "success";
}
}

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file defines how elements are processed. The entry of FilterDispatcher is done in the web.xml file. This file is created in WebContent->WEB-INF folder. /* specifies all urls will be parsed. This task is done by struts filter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsBundledValidation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Construct struts.xml file:

In the struts.xml file, make the entry of the action class LoginAction and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success, input and error.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<package name="default" extends="struts-default">  
  
<action name="login" class="LoginAction">  
<result name="success">welcome.jsp</result>  
<result name="error">error.jsp</result>
<result name="input">index.jsp</result>
</action>  
</package>  
</struts>

Create the validation file:

Create the LoginAction-Validation.xml file to validate email, DOB, and password length. It allows the date to be entered between a specified range.

<?xml version="1.0" encoding="UTF-8"?>  
  
  <!DOCTYPE validators PUBLIC   
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  
        <validators>  
          
        <field name="email">  
        <field-validator type="requiredstring">  
        <message>Email can not be blank</message>  
        </field-validator>  
        <field-validator type="email">  
        <message>Email entered is wrong...Please, Enter correct email.</message>  
        </field-validator>  
        </field>
        
        <field name="pwd">  
        <field-validator type="requiredstring">  
        <message>Password can't be blank</message>  
        </field-validator>  
        <field-validator type="stringlength">  
        <param name="minLength">6</param>  
        <param name="maxLength">10</param>  
        <message>Password must be greater than or equal to 6 and less than or equal to 10</message>  
        </field-validator> 
        </field>
        
          <field name="dob">  
        <field-validator type="date">  
        <param name="min">01/01/1950</param>  
        <param name="max">01/01/2002</param>  
          
        <message>Date of Birth must be between ${min} to ${max}</message>  
        </field-validator>  
        </field>  
          
                 
        </validators>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show three labels for entering the email, DOB and password, and the Register button.

Struts Bundled Validators - date

Enter any email and enter the password whose length is between 6 and 10 and date after 01/01/2002

Struts Bundled Validators - date

As the date is not within the range, the error message will display.

Struts Bundled Validators - date

Again run the application. This time enter any email, password and enter the date before 01/01/1950

Struts Bundled Validators - date

As the date is not within the range, the error message will display.

Struts Bundled Validators - date

Again run the application. This time enter any email and enter a password whose length is greater than 6 and less than 10. Enter the date within the range 01/01/1950 to 01/01/2002

Struts Bundled Validators - date

As the email, DOB and password are entered correctly, execute method will return a success string. It will redirect to page welcome.jsp and display a Welcome message.

Struts Bundled Validators - date

Related Topics

Struts 2 Multiple Configuration file

In order to create large struts web applications, multiple configuration files are used. Multiple configuration files are included in struts.xml file using include sub-element. Struts- Multiple Configuration File Example- To get input...

5 minutes read.

Struts 2 Database Access

The database can be connected with struts applications to store and retrieve data using JDBC. Struts Database Access example: Create the database and create the table in it: Create database stuser;  // create...

4 minutes read.

Struts 2 prepare Interceptor

In order to implement prepare interceptor in the struts web application, the class must implement a Preparable interface and override the prepare method along with the execute method. While executing...

4 minutes read.

Struts 2 Generator tag

The generator tag in struts is used to iterate over the collection of elements provided by the val attribute. It allows us to iterate through every element and perform the...

4 minutes read.

Struts 2 Form tag

In struts, web application various UI elements tag are used to create forms like HTML. Various struts UI tags like form tag, textfield tag, password tag, textarea tat, checkbox tag,...

6 minutes read.

Struts 2 If Else tag

In the struts, web application, the If Else tag is used to check among multiple conditions. The code along with the right condition will be executed as the result. Struts Control...

4 minutes read.

Struts 2 url tag

The url tag in struts is used to create url as a text. It creates url as a text string. Struts Data tags- url tag Example: Create the index.jsp file showing the...

4 minutes read.

Struts 2 Interceptors

Most of the processing in the framework is done by the interceptor. At the time of pre-processing and post-processing of the request, interceptors are invoked. Different functions such as exception...

3 minutes read.

Struts 2 Date Validator

In the struts web application, a date validator is used to check whether the entered date is within the specified range. Three parameters defined with date validator are: fieldName: It is...

5 minutes read.

Struts 2 property tag

The property tag in struts is used to get values of the fields set in the bean class. Struts Data tags- Property tag Example: Create the index.jsp file showing the use of...

4 minutes read.

Struts 2 Installation and Example

Struts Installation: For developing struts applications, you need to have java installed on your machine. For verifying that Java is already installed on your machine, use the following commands: For Windows: Open Command line.Type...

4 minutes read.

Struts 2 Stringlength Validator

In the struts web application, stringlength validator is used to check whether the length of the string is between the minimum and maximum length. Four parameters defined with stringlength validator...

5 minutes read.

Struts 2 params Interceptor

Parameters Interceptor is also known as params interceptor implemented in struts application by implementing Interceptor interface. We have to override all three life cycle methods. The Interceptor interface itself provides...

4 minutes read.

Struts 2 exception Interceptor

In the struts web application, an exception may occur at any point. This exception should be handled properly. The global exception handling feature is provided by struts. With the help...

4 minutes read.

Struts 2 param tag

The param tag in struts is used to set values of the variable in other tags like include and bean tag. It is used to parametrize other tags. Struts param...

4 minutes read.

Struts 2 Tutorial

Struts is mainly used to create java-based applications. It is the complete framework used in activities ranging from development to deployment and maintenance. Struts is the MVC (Model View Controller)...

4 minutes read.

Struts 2 include tag

The include tag in struts is used to include some other resource like other jsp or servlet file in the existing jsp page. In the struts web application, it is...

4 minutes read.

Struts 2 Fetch Records From Database

The database can be connected with struts applications to retrieve data using JDBC. Struts-Fetch Records From Database example: Create the database and create the table in it: Create any database using sql command...

4 minutes read.

Struts 2 execAndWait interceptor

The execute and wait interceptor is also known as execAndWait interceptor. The intermediate result is displayed with the help of this interceptor. The wait result is specified in the struts.xml...

4 minutes read.

Struts 2 Datetimepicker tag

The datetimepicker tag in struts is used when the user needs to take date input. It displays a calendar icon. When we click on this icon, it allows us to...

4 minutes read.