Since version 0.4 , Struts-Layout allows the page author to call a specific format method when displaying the property of a bean. This permits for example to format a date or a number according to the user locale or preference, or to generate a mailto link when displaying a user name.
To use struts-layout formatting functionnality, a formatter class must be implemented. Two classes can be extended:
public String format(Object in_value, String in_format, javax.servlet.jsp.PageContext in_pageContext) throws fr.improve.struts.taglib.layout.formatter.FormatException
String myFormat(Object in_value, PageContext in_pageContext)
Simple example to display a localized date and a mailto link to a user email (supposing the User class has a method getName() and getEmail()).
import java.util.Date;
import java.util.Locale;
import java.texte.DateFormat;
import javax.servlet.jsp.PageContext;
import fr.improve.struts.taglib.layout.formatter.*:
public class MyFormatter extends DispatchFormatter {
public String date(Object in_date, PageContext in_pageContext) {
Date lc_date = (Date) in_date;
DateFormat lc_format = DateFormat.getDateInstance(DateFormat.SHORT, in_pageContext.getRequest().getLocale());
return lc_format.format(lc_date);
}
public String user(Object in_user, PageContext in_pageContext) {
User lc_user = (User) in_user;
StringBuffer lc_buffer = new StringBuffer();
lc_buffer.append("<a href=\"mailto:");
lc_buffer.append(lc_user.getEmail());
lc_buffer.append("\">");
lc_buffer.append(lc_user.getName());
lc_buffer.append("</a>");
return lc_buffer.toString();
}
}
Once the formatter class is implemented, struts-layout must be configured to use this class. This is done by specifying the name of the formatter class in the skin configuration file. If you're using the default skin, you need to put a file name Struts-Layout_default.properties in the WEB-INF/classes folder of the webapp. If you're using other skins properties files, you need to put the property in each Struts-Layout_skinName.properties file.
Example (Struts-Layout_default.properties):
formatter.class = com.mycompany.MyFormatter
The <layout:write> and the <layout:collectionItem> tag supports a special type attribute. When this attribute is set, those tags will ask the formatter to format the value using the type specified.
Example:
<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
<%@page import="java.util.Date,java.util.ArrayList" %>
<html>
<head>
<layout:skin/>
<% /* Create a list of date */
ArrayList lc_list = new ArrayList();
lc_list.add(new Date(1016617298917L));
lc_list.add(new Date(1026817298917L));
lc_list.add(new Date(1037017298917L));
lc_list.add(new Date());
pageContext.setAttribute("dates", lc_list);
%>
</head>
<body>
<!-- Non formatted output -->
<layout:collection name="dates" styleClass="FORM" width="150">
<layout:collectionItem title="non formated date"/>
</layout:collection>
<br />
<!-- collectionItem tag formatted output -->
<layout:collection name="dates" styleClass="FORM" width="150">
<layout:collectionItem title="formated date" type="date"/>
</layout:collection>
<br />
<!-- write tag formatted output -->
<layout:collection name="dates" styleClass="FORM" width="150" id="date">
<layout:collectionItem title="formated date"><layout:write name="date" type="date"/></layout:collectionItem>
</layout:collection>
</body>
</html>
This will display a list of date formatted using the date format method and the user browser language preference.