Thursday, 14 April 2016

How to use datetimepicker in Struts 2 ?


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.

Action class

package org.shaan.example;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;

public class DateBean extends ActionSupport {
  public String execute() throws Exception {
    setTodayDate(new Date());
    return SUCCESS;
  }

  private Date todayDate;
  public Date getTodayDate() {
    return todayDate;
  }
  public void setTodayDate(Date value) {
    todayDate = value;
  }
}


Entry in struts.xml

<action name="DateTimePicker" class="org.shaan.example.DateBean">
    <result>/pages/datepicker.jsp</result>
</action>


JSP file

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
   <title>Struts 2 Format Date Example!</title>
   <link href="<s:url value=" 
         mce_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:form>
</body>
</html>

The <s:head theme="ajax" /> generates the client side dojo specific javascript.



Format by datepicker component

dd Day in two digits format
d Day in one digit format, if cannot use 2 digit format
MM Month in two digits format
M Month in one digits format, if cannot use 2 digit format
yyyy Year in four digits format
yy Last two digits of the year
y Last digit of the year

No comments:

Post a Comment

Note: only a member of this blog may post a comment.