Struts Tip #4 - Formatting output with MessageResources

Ted Husted

 

 

A common requirement of the presentation layer is to format information in a human-friendly way. While a numeral rendered as 1000000 works well for the software, most humans can't turn that data into information at a glance. Humans expect a numeral to be rendered as 1,000,000 - which they can interpret at a glance to be the value one million.

At least to Americans.

People in other countries might find the name rather strange, since it includes two decimal points, or what an American would render as 1.000.000.

Since many Struts applications are localized, this are important issues. When a page in a Struts application renderes a number, or a date, or a percentage, it should be able to do so according to each user's locale.

Struts already has localized messaging built-in, which most developers use for error messages, prompts, and field label names. A Struts applications refers to messages like this using a symbolic key, which is used to retrieve the actual messages from the Struts application resource bundle. The messages can also be used as templates, and the application can pass custom values to them at runtime. Often, there is a default template for an entire class of message, and the field name or other value is merged into the static text at runtime. Each locale can have it's own resource properties file, which

Now, it's important to note that Struts didn't invent any of this. Resource bundles, message templates, locales, and the rest of it are all part of the Java internationalization framework. Struts makes Java i18n an intregal part of its own framework, and provides support classes making it easier to use. But the core functionality is provided by the Java internationalization framework.

Including the abilty to apply formatting to a value a runtime.

Most message templates are created by just indicating the placeholder, like

ordering.authorized.range.staff=Staff is only authorized to requisition supplies that cost less than ${0}

It then becomes the application's responsiblility to supply whatever string is needed to replace {0}, which will be inserted into the message template, as-is.

However, you can also pass more than one parameter as the placeholder, and also include one of the standard message format classes: number, currency, percent - along with a default mask. This means you can just as easily render the template as

ordering.authorized.range.staff=Staff is only authorized to requisition supplies that cost less than {0,currency}

which will insert the appropriate thousands-delimter or decimal-separator for the user's locale. This example would also insert the monetary symbol for the user's country. If everything should be expressed in a single currency, then you'd use number instead

ordering.authorized.range.staff=Staff is only authorized to requisition supplies that cost less than ${0,number} USD

The same technique can be applied to dates and percentages. See the Java Internationalization Tutorial's page on the MessageFormat class for more

While the Struts application resource bundle is most often used when generating error messages, the <bean:message> tag can be used to access message templates throughout your application, and pass the replacement parameters at runtime to generate a customized display.

Here's the same example message coded for <bean:message>

<bean:message key="ordering.authorized.range.staff" arg0='<%= config.getStaffAuthAmnt() %>'/>

where there is a bean in some scope named "config" that can return the amount in question.

HTH, Ted.

-----

Struts Tips  are excerpts from the book Java Web Development with Struts. The tips released twice weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering.

About Ted. Ted Husted is an active Struts Committer and co-author of Struts in Action and Professional JSP Site Design. Ted also moderates the Struts mailing list and the JGuru Struts FAQ.

Copyright Ted Husted 2002. All rights reserved.

---

MVC-Programmers mailing list
MVC-Programmers@basebeans.com
http://www.basebeans.com:8081/mailman/listinfo/mvc-programmers

 

 

 

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact