2.6. Using Multiple Resource Bundles

Problem

You want to break apart your application resources properties file into multiple files for improved organization and easier maintenance, particularly in a team environment.

Solution

Create separate properties files and declare a message-resources element for each file in your struts-config.xml file:

<message-resources
  parameter="com.oreilly.strutsckbk.MessageResources"/>
<message-resources 
  parameter="com.oreilly.strutsckbk.LabelResources"
  key="labels">
</message-resources>
<message-resources 
  parameter="com.oreilly.strutsckbk.HeaderResources"
  key="headers">
</message-resources>

Discussion

Struts uses a concept known as message resources to provide a mechanism for storing error messages, field labels, and other static text. With the default Struts implementation, you store the messages as name/value pairs in a .properties file. A message resources set is basically the same as a Java ResourceBundle.

You make your message resources properties file available to Struts using the message-resources element. The parameter attribute identifies the classpath-relative name of the properties file. You derive the value for this attribute by replacing the path separator in the file's path with a dot (".") and removing the .properties extension from the filename. For example, if the properties file was located in /WEB-INF/classes/com/oreilly/strutsckbk/MessageResources.properties, you would set up the message resources element as follows:

<message-resources ...

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.