Text formatting

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.

Implementing a formatter class

To use struts-layout formatting functionnality, a formatter class must be implemented. Two classes can be extended:

  • fr.improve.struts.taglib.layout.formatter.AbstractFormatter. In this case the following method must be implemented:
public String format(Object in_value, String in_format, javax.servlet.jsp.PageContext in_pageContext) throws fr.improve.struts.taglib.layout.formatter.FormatException
  • fr.improve.struts.taglib.layout.formatter.DispatchFormatter. The format method is already implementd in this class and will use reflection to call a method with the name of the format. The method signature must be:
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()).

 1  import java.util.Date;
 2  import java.util.Locale;
 3  import java.texte.DateFormat;
 4
 5  import javax.servlet.jsp.PageContext;
 6
 7  import fr.improve.struts.taglib.layout.formatter.*:
 8
 9  public class MyFormatter extends DispatchFormatter {
10
11     public String date(Object in_date, PageContext in_pageContext) {
12         Date lc_date = (Date) in_date;
13         DateFormat lc_format = DateFormat.getDateInstance(DateFormat.SHORT, in_pageContext.getRequest().getLocale());
14         return lc_format.format(lc_date);
15     }
16     public String user(Object in_user, PageContext in_pageContext) {
17         User lc_user = (User) in_user;
18         StringBuffer lc_buffer = new StringBuffer();
19         lc_buffer.append("<a href=\"mailto:");
20         lc_buffer.append(lc_user.getEmail());
21         lc_buffer.append("\">");
22         lc_buffer.append(lc_user.getName());
23         lc_buffer.append("</a>");
24         return lc_buffer.toString();
25     }
26  }

Configuring struts-layout

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

Using the formatter tags

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:

 1<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
 2<%@page import="java.util.Date,java.util.ArrayList" %>
 3<html>
 4
 5<head>
 6<layout:skin/>
 7<% /* Create a list of date */
 8   ArrayList lc_list = new ArrayList();
 9   lc_list.add(new Date(1016617298917L));
10   lc_list.add(new Date(1026817298917L));
11   lc_list.add(new Date(1037017298917L));
12   lc_list.add(new Date());
13   pageContext.setAttribute("dates", lc_list);
14%>
15</head>
16
17<body>
18
19<!-- Non formatted output -->
20<layout:collection name="dates" styleClass="FORM" width="150">
21    <layout:collectionItem title="non formated date"/>
22</layout:collection>
23
24<br />
25
26<!-- collectionItem tag formatted output -->
27<layout:collection name="dates" styleClass="FORM" width="150">
28    <layout:collectionItem title="formated date" type="date"/>
29</layout:collection>
30
31<br />
32
33<!-- write tag formatted output -->
34<layout:collection name="dates" styleClass="FORM" width="150" id="date">
35    <layout:collectionItem title="formated date"><layout:write name="date" type="date"/></layout:collectionItem>
36</layout:collection>
37
38</body>
39</html>

This will display a list of date formatted using the date format method and the user browser language preference.

Result