Struts 2 datetimepicker example
In Struts 2, the dojo ajax tag “<sx:datetimepicker>” will render a text box and append a calender icon behind, click on the calender icon will prompt a date time picker component.
To create a date time pick component, just ensure :
1. Download the struts2-dojo-plugin.jar library.
2. Include the “struts-dojo-tags” tag and output its header.
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head>
For example,
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head> <body> <sx:datetimepicker name="date2" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy" value="%{'2010-01-01'}"/> ...
Resulting the following HTML, few dojo and JavaScript libraries to create a date time pick component.
<html> <head> <script language="JavaScript" type="text/javascript"> // Dojo configuration djConfig = { isDebug: false, bindEncoding: "UTF-8" ,baseRelativePath: "/Struts2Example/struts/dojo/" ,baseScriptUri: "/Struts2Example/struts/dojo/" ,parseWidgets : false }; </script> <script language="JavaScript" type="text/javascript" src="/Struts2Example/struts/dojo/struts_dojo.js"></script> <script language="JavaScript" type="text/javascript" src="/Struts2Example/struts/ajax/dojoRequire.js"></script> <link rel="stylesheet" href="/Struts2Example/struts/xhtml/styles.css" type="text/css"/> <script language="JavaScript" src="/Struts2Example/struts/utils.js" type="text/javascript"></script> <script language="JavaScript" src="/Struts2Example/struts/xhtml/validation.js" type="text/javascript"></script> <script language="JavaScript" src="/Struts2Example/struts/css_xhtml/validation.js" type="text/javascript"></script> </head> ... <td class="tdLabel"> <label for="widget_1291193434" class="label">Format (dd-MMM-yyyy): </label></td> <td> <div dojoType="struts:StrutsDatePicker" id="widget_1291193434" value="2010-01-01" name="date2" inputName="dojo.date2" displayFormat="dd-MMM-yyyy" saveFormat="rfc"></div> </td> </tr> <script language="JavaScript" type="text/javascript"> djConfig.searchIds.push("widget_1291193434");</script>
Struts 2 <s:datetimepicker> example
A complete full example of the <s:datetimepicker> tag to generate a datetimepicker component, and show the use of OGNL and Java property to set the default date to the “datetimepicker” component.
1. pom.xml
Download the Struts 2 dojo dependency libraries.
pom.xml
//... <!-- Struts 2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> <!-- Struts 2 Dojo Ajax Tags --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-dojo-plugin</artifactId> <version>2.1.8</version> </dependency> //...
2. Action class
Action class to store the selected date.
DateTimePickerAction.java
package com.mkyong.common.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class DateTimePickerAction extends ActionSupport{ private Date date1; private Date date2; private Date date3; //return today date public Date getTodayDate(){ return new Date(); } //getter and setter methods public String execute() throws Exception{ return SUCCESS; } public String display() { return NONE; } }
3. Result Page
Render the date time picker component via “<s:datetimepicker>” tag, set the default date via Java property and OGNL.
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head>
datetimepicker.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head> <body> <h1>Struts 2 datetimepicker example</h1> <s:form action="resultAction" namespace="/" method="POST" > <sx:datetimepicker name="date1" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy" value="todayDate" /> <sx:datetimepicker name="date2" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy" value="%{'2010-01-01'}"/> <sx:datetimepicker name="date3" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy" value="%{'today'}"/> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 datetimepicker example</h1> <h4> Date1 : <s:property value="date1"/> </h4> <h4> Date 2 : <s:property value="date2"/> </h4> <h4> Date 3 : <s:property value="date3"/> </h4> </body> </html>
3. struts.xml
Link it all ~
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="dateTimePickerAction" class="com.mkyong.common.action.DateTimePickerAction" method="display"> <result name="none">pages/datetimepicker.jsp</result> </action> <action name="resultAction" class="com.mkyong.common.action.DateTimePickerAction" > <result name="success">pages/result.jsp</result> </action> </package> </struts>
4. Demo
http://localhost:8080/Struts2Example/dateTimePickerAction.action









Hi Mykong ,
When I try to implement the above example , get the below error
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Gshax] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/logging/LoggerFactory] with root cause
java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.logging.LoggerFactory
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.struts2.dojo.components.DateTimePicker.(DateTimePicker.java:184)
at org.apache.struts2.dojo.views.jsp.ui.DateTimePickerTag.getBean(DateTimePickerTag.java:59)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:49)
at org.apache.jsp.Hfdadd_jsp._jspx_meth_sx_005fdatetimepicker_005f0(Hfdadd_jsp.java:297)
at org.apache.jsp.Hfdadd_jsp._jspx_meth_s_005fform_005f0(Hfdadd_jsp.java:149)
at org.apache.jsp.Hfdadd_jsp._jspService(Hfdadd_jsp.java:96)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:416)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Also,
Can you pls let me know where to put pom.xml in eclipse.
I mean, I was following your other example I am stuck with this one.
Can’t I make this example work without pom.xml ?
This is good example.
But when i setup this example i meet one error: i input value into date1 field by keyboard, i do not use calendar. But i move focus other element, that value is lost. Thus i can not input value by hands.
Please help me!
Hi,
Thanks your articles are nice and helped me lot on many instances. I was trying to use the DOJO validations and it works fine when I use normal text type elements of the html.
However I need to use this with the struts text boxes can you tell me how should I go for this. An approach will really help me.
Regards,
Jayesh
gr8 work.
Thanks for this nice example.
I am using struts 2 with hibernate.
I have a two date time picker fields in my JSP.one for joining date and other for relieving date and a checkbox “I currently work here”.when the user checks the checkbox i want to disable the Relieving date time picker field.
Please help.
Just use JavaScript or jQuery to disabled the component.