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

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.