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 choose date and time with date time picker.

Struts datetimepicker tag Example:

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

The index.jsp page has heading Struts datetimepicker UI tags example. It contains one form with link to the Test class. The form contains date time picker with calendar icon and label Select your Birth Date. The display format for input date is dd-MM-yyyy. At the last of the form there is one submit button to submit date value entered by the user.

<%@ 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>
<title> Struts 2 datetimepicker UI tags example </title>
</head>
<body>
<h3> Struts datetimepicker UI tags example. </h3>
 
<s:form action= "test">
<sx:datetimepicker name= "birthdate"  
                   label=" Select your Birth Date: "
           displayFormat= "dd-MM-yyyy" />
<s:submit value= "Submit" align= "center"/>
</s:form>
 
</body>
</html>

Create welcomes.jsp file to denote success:

The welcome.jsp file display heading Struts datetimepicker UI tags example and it shows the birthdate selected by the user with the label Your Birth Date is followed by birth date of the user.

<%@ 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>
<title> Struts 2  datetimepicker UI tags example </title>
</head>
<body>
<h3> Struts datetimepicker UI tags example </h3>
 
Your Birth Date is : <s:property value= " birthdate " /> 
 
</body>
</html>

Create error.jsp file to denote failure:

The error.jsp page will display Sorry something went wrong !!! if something went wrong.

<%@ 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> Insert title here </title>
</head>
<body>
Sorry something went wrong !!!
</body>
</html>

Create the class to include execute method:

Java class with the name test is created which extends the ActionSupport class. This class contains execute() method which will return a success string on successful execution. Also, it contains one field birthdate with its getters and setters to set and retrieve the date entered by the user.

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;


public class Test extends ActionSupport
{


private Date birthdate;


public String execute()
{
return SUCCESS;
}
public Date getBirthdate () 
{
return birthdate;
}


public void setBirthdate (Date birthdate)
 {
this. birthdate = birthdate;
}
}

Construct struts.xml file:

In the struts.xml file, make the entry of the action class 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 and error. On success, it will redirect to welcome.jsp page and on error, it will redirect to error.jsp page.

<?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= "test" class= "Test">
<result name= "success"> /welcome.jsp </result>
<result name= "error"> /error.jsp </result>


</action>
</package>
 
</struts>

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

The web.xml file in the WEB-INF folder specify how elements are processed. The entry of FilterDispatcher is done in the web.xml file. /* 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> StrutsDateTime </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>

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 run the index.jsp page showing the heading Struts datetimepicker UI tags example. It contains one label to Select your Birth Date and shows one calendar icon. At the last of the form, there is submit button to submit the data entered by the user.

Struts datetimepicker tag

Select your birth date by clicking on the calendar icon. It shows calendar, select date, month, year, and click on submit button.

Struts datetimepicker tag

After successful selection, it will redirect to welcome.jsp file showing heading, Struts datetimepicker UI tags example and label Your Birth Date is followed by birth date entered by the user.

Struts datetimepicker tag