Magazine
 
Struts2 Data Tags

beanTag.jsp

<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Bean Tag Example</title>
</head>
<body>

Output of the beanTag.jsp

3. Date Tag (Data Tag) Example

The date tag allows formatting a Date in a quick and easy way. User can specify a custom format (eg. “dd/MM/yyyy hh:mm”), can generate easy readable notations (like “in hours, 14 minutes”), or can just fall back on a predefined format with key ‘struts.date.format’ in the properties
file. If that key is not defined, it will finally fall back to the default DateFormat.MEDIUM formatting.

Note: If the requested Date object isn’t found on the stack, a blank will be returned.

Configurable attributes are:

  1. name
  2. nice
  3. format

Add the following code snippet into the “struts.xml” file.
 

struts.xml

<action name=”dateTag”
class=”net.javajazzup.dateTag”>
<result>/pages/dataTags/dateTag.jsp
</result>
</action>

Create an action class as shown below:

dateTag.java

package net.javajazzup;
import
com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class dateTag extends ActionSupport {
private Date currentDate;
public String execute() throws Exception{
setCurrentDate(new Date());
return SUCCESS;
}
public void setCurrentDate(Date date){
this.currentDate = date;
}
public Date getCurrentDate(){
return currentDate;
}
}

Now create a jsp page using <s:date> tag as shown in the success.jsp page.

The <s:date name=”currentDate” format=”dd/ MM/yyyy” /> date tag formats a Date in a quick and easy way. Here the “format” parameter specifies a custom format (eg. “dd/MM/yyyy hh:mm”) to follow.

The nice parameter is of Boolean type, which
decides whether to print out the date nicely or
not. By Default it is kept false which prints out
date nicely i.e. <s:date name=”currentDate” nice=”false” /> tag formats a date and similarly <s:date name=”currentDate” nice=”true” /> does not format a date, it is illustrated in our current jsp page.

Jan 2008 | Java Jazz Up | 44
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF