Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.  

                         


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

Current Comments

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

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

Yeah, got the same problem even in its latest version. I'm having an exception that says it doesn't found any DatePicker action.

Posted by psycotrompus on Friday, 01.18.08 @ 06:35am | #45489

<s:datetimepicker id="date" name="date" value="${date}"/>
<script>
.....
if(document.getElementById("date").value==""){
alert("You Must Chose The Date");
}
.....
</script>
but i can't get it's value ,how to get it's value

Posted by SavageGarden on Monday, 12.17.07 @ 08:40am | #42670

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

Indian Software Development Company

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

Copyright © 2007. All rights reserved.