Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

12.6. Displaying Locale-Specific Text

Problem

Your Struts application needs to display correctly formatted text—particularly numbers, dates, and messages—based on a user's locale.

Solution

From the Struts bean tag library, use the following:

<%-- Format a number --%>
<bean:write name="beanName"
        property="numericProperty"
          format="number pattern"/>

<%-- Format a date --%>
<bean:write name="beanName"
        property="dateProperty"
          format="date pattern"/>

<%-- Format a message with parameters --%>
<bean:message key="message.key"
             argn="replacement value"/>

From the JSTL fmt tag library, use the following:

<%-- Format a number --%>
<fmt:formatNumber value="${beanName.numericProperty}"
                pattern="number pattern"/>     

<%-- Format a date --%>
<fmt:formatDate value="${beanName.dateProperty}"
              pattern="date pattern"/>

<%-- Format a message with parameters --%>
<fmt:message key="message.key">
    <fmt:param value="replacement value"/>
</fmt:message>

Discussion

Struts provides the generic bean:write tag to output text formatted for a specific locale and bean:message to render localized messages. If you're using JSTL, you can use the tags of the fmt tag library.

Using the Struts bean tags

This bean:write tag renders a value specified by the standard Struts name and property attributes. This tag can format dates and numbers before outputting the value using the pattern specified by the format attribute. The format pattern will be applied if the value is a java.lang.Number or java.util.Date object. Numbers are formatted ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page