DatePicker Example Using Struts2 API

Posted By : Rahul Singh | 22-Apr-2018

Hi Guys, I am discussing here the Struts2 DateTimePicker, Which is the common feature of any application to add Date Picker. Struts2 have many styles of DatePicker options.


The Struts2 framework provides a high level and portable tag API that you can use with JSP. Here I explained some basic tag in struts2 API Which will understand the structure of struts2. You will learn how the tag work and how to use OGNL to reference values on the ValueStack in the section that follow:


Ajax Tags: Struts2 provides Ajax support tags.

Control Tags: These are tags that provide ways to manipulate collections of elements.

Data Tags: These are tags that render data from the action, internationalized text, and URLs.

Form Tags: These are a tag that provides a wrapper for Html form tags, as well as additional widgets such as a date picker.

Non-form UI tags:  The tags in this group are used in forms but are not directly from entry elements. They include error message displays, tabbed panels, and tree views.
 
These are the tags which are using struts2 to provide better development of a website. We use Form Tag in this blog to provide better User Interface for Date selector called DatePicker.

 

Struts2 DateTimePicker Example

 

1) Create index.jsp for input

Here we create a ".jsp" file which can take input from User in the form of struts2 DatePicker. This is totally a readymade UI for choose Date and Time.

 

<%@ taglib prefix = "html" uri = "/struts-tags" %>  
<%@ taglib prefix = "sTags" ur i ="/struts-dojo-tags" %>  

< html:form action="DateTimePicker" method="POST">  
  
  
< html:submit>
< /html:submit>  

 

 

2) Create the action class RegisterAction.java

Here, we create a bean which can hold (means get and set date) date value and this class extend by "ActionSupport".

 

package mypack;  
import com.opensymphony.xwork2.ActionSupport;  
import java.util.Date;  
public  class DateBean  extends ActionSupport {  
    private Date date1,date2;  
        public Date getDate2() {  
        return date2;  
    }  
    public void setDate2(Date date2) {  
        this.date2 = date2;  
    }  
  
    public Date getdate() {  
        return date;  
        }  
        public void setDate(Date value) {  
        date = value;  
        }  
      
       public String execute(){  
        return SUCCESS;  
       }  
}  

 

3) Create struts.xml struts.xml

Here, we can create the XML file which can manage bean life cycle (means create bean class instance and redirect the result -"welcome.jsp") of our mean class.

 

/welcome.jsp

4) Create view component welcome.jsp

This file shows the result.

 

<%@ taglib prefix = "sTags" uri = "/struts-tags" %>  
First Date Example:  
Second date Example:  
 

 

UI of DatePicker :


datepicker image

 

 

There are some Date and Time Pattern:

 

Pattern Result (in a particular locale)
yyyy.MM.dd G 'at' HH:mm:ss zzz 2018.04.18 AD at 15:08:56 PDT
EEE, MMM d, ''yy Wed, April 10, '18
h:mm a 10:05 PM
hh 'o''clock' a, zzzz 10 o'clock PM, Pacific Daylight Time
K:mm a, z 0:00 PM, PST
yyyyy.MMMM.dd GGG hh:mm aaa 02018.April.18 AD 10:05 PM

 

---Thanks--

About Author

Author Image
Rahul Singh

Rahul singh is a Java Developer and having experience in developing Applications. He is a quick learner.

Request for Proposal

Name is required

Comment is required

Sending message..