Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions? | Software Development
 

Struts 2 datetimepicker Example

In this section we will show you how to develop datetimepicker in struts 2. Struts 2 uses the dojo toolkit for creating date picker. In Struts 2 its very easy to create date time picker.

Struts 2 datetimepicker Example

                         

In this section we will show you how to develop datetimepicker in struts 2. Struts 2 uses the dojo toolkit for creating date picker. In Struts 2 its very easy to create date time picker.

The <s:datetimepicker .../> tag

The Struts 2 <s:datetimepicker ...> actually renders date/time picker in the dropdown container. When user clicks on the calendar icon the date/time picker is displayed on the form.

Struts 2 date picker is actually Dojo widget, that makes it easy to select a date. It also provides easy way to select any date/month/year very fast. The date time picker will make your web application very user friendly.

Writing time picker code

Action class to get today's date is DateBean. Here is the code of DateBean.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;


/**
 <p> Validate a user login. </p>
 */
public  class DateBean  extends ActionSupport {

  

    public String execute() throws Exception {

      setTodayDate(new Date());
      return SUCCESS;

  }


    // ---- Username property ----

    /**
     <p>Field to store Today's Date.</p>
     <p/>
     */
    private Date todayDate;


    /**
     <p>Provide Today's Date.</p>
     *
     @return Returns the Todays date.
     */
    public Date getTodayDate() {
        return todayDate;
    }

    /**
     <p>Store new Date</p>
     *
     @param value The username to set.
     */
    public void setTodayDate(Date value) {
        todayDate = value;
    }

}

Add the following entry in the struts.xml file in order to create action mapping:

<action name="DateTimePicker" class="net.roseindia.DateBean">
<result>/pages/datepicker.jsp</result>
</action>

Following jsp file(datepicker.jsp) shows the usage of datepicker tag: 

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Format Date Example!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

<s:head theme="ajax" />

</head>
<body>


<s:form action="DateTimePicker" method="POST">

<s:datetimepicker name="todayDate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/>

<s:datetimepicker name="todayDate" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy"/>



</s:form>

</body>

</html>

Struts 2 date picker is dojo widget so, it is necessary to generate the client side JavaScript code. The <s:head theme="ajax" />  generates the client side dojo specific java script. Here is the code generated by this tag:

	  <link rel="stylesheet" href="/struts2tutorial/struts/xhtml/styles.css" type="text/css"/>
<script language="JavaScript" type="text/javascript">
    // Dojo configuration
    djConfig = {
        baseRelativePath: "/struts2tutorial/struts/dojo",
        isDebug: false,
        bindEncoding: "UTF-8",
        debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
    };
</script>
<script language="JavaScript" type="text/javascript"
        src="/struts2tutorial/struts/dojo/dojo.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/struts2tutorial/struts/simple/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/struts2tutorial/struts/ajax/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/struts2tutorial/struts/CommonFunctions.js"></script>

The code:

<s:datetimepicker name="todayDate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/>

generates the date/time picker on the form.

Following table summarizes the format by datepicker component:
Format Description
dd This format is used to display day in two digits format
d Try to display day in one digit format, if cannot use 2 digit format
MM The format displays month in two digits format
M It try to display month in one digits format, if cannot use 2 digit format
yyyy Display year in four digits format
yy Display the last two digits of the year
y Display the last digits of the year

Here is the output of the program

 

In this section we have studied how to use Struts 2 datepicker control.  

                         


 
» View all related tutorials
Related Tags: c web datetime ide orm date select form time application dojo calendar struts io make user get pdo display icon

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

16 comments so far (
post your own) View All Comments Latest 10 Comments:

The jsp dsplays me the label: Format (yyyy-MM-dd): , but not the datetimepicker . I don't know why??

Posted by lotfi on Tuesday, 12.8.09 @ 20:25pm | #93186

How to customise input of date format

Posted by Manohar Singh on Tuesday, 09.23.08 @ 16:04pm | #80632

Hi ,

Default size of Calendar can not be viewed properly. How can I increase Calendar Image size. I tried xssClass and cssStyle attributes but did not work. Can you provide example to css file to increase size of Calendar.

I appreciate your help.

Thanks,
Venkata.

Posted by venkata on Monday, 08.4.08 @ 20:59pm | #70964

How To Customise The Calendar, Struts Struts2 Date Struts 2 Datetimepicker

Posted by sathya on Wednesday, 06.18.08 @ 15:56pm | #63724

How to increase size of calender? So that it can be viewed easily. Thanks

Posted by imra khan on Friday, 05.23.08 @ 16:46pm | #60756

Hi

Im trying to link the output of the calendar in one Go button in order to check the site with the same name in my web server but i cant.
Can you give to me any sugestion.
Thanks

Posted by elvis on Thursday, 05.15.08 @ 15:02pm | #60027

Hi,
I want to know if it 's possible to allow multiple selections in same datetimepicker struts2 component? (to select more than one date)
thanks

Posted by ziko on Thursday, 04.3.08 @ 18:57pm | #55237

Hello,

JS files are included in Struts 2 framework.

Following code is automatically generated:
<link rel="stylesheet" href="/struts2tutorial/struts/xhtml/styles.css" type="text/css"/>
<script language="JavaScript" type="text/javascript">
// Dojo configuration
djConfig = {
baseRelativePath: "/struts2tutorial/struts/dojo",
isDebug: false,
bindEncoding: "UTF-8",
debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
};
</script>
<script language="JavaScript" type="text/javascript"
src="/struts2tutorial/struts/dojo/dojo.js"></script>

<script language="JavaScript" type="text/javascript"
src="/struts2tutorial/struts/simple/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
src="/struts2tutorial/struts/ajax/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
src="/struts2tutorial/struts/CommonFunctions.js"></script>

So, there is no need to include additional js file.

Posted by Deepak on Monday, 03.31.08 @ 20:41pm | #54883

Hi,

Please help me. I have added the dojo folder and i am calling the s:datetimepicker, i am getting 'dojo' undefined error. The JS file that i am calling is getting included. I have given an alert in that and checked.

Thanks,
Varadh

Posted by Varadh on Monday, 03.24.08 @ 03:06am | #54000

Where are the js files used in the above code?

Posted by Varadh on Sunday, 03.23.08 @ 02:38am | #53846

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.