O'Reilly logo

Jakarta Struts Cookbook by Bill Siggelkow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required