Tuesday, October 28, 2008

Multiple Struts Resource Bundles

The process of adding multiple resource bundles to a single Struts 1.35 webapp can be a little cryptic. This should help clear things up.

struts-config:
The first thing you need to do is to define the resource bundles in your struts-config.xml file. The typical struts-config file contains a line that defines the default resource bundle:

<message-resources parameter="resources.application"/>

To add an additional bundle we need to assign it a key as well as a parameter like so:

<message-resources key="registration" parameter="resources.registration"/>

Our complete struts-config now contains the following lines:

<message-resources parameter="resources.application"/>
<message-resources key="registration" parameter="resources.registration"/>

Properties Files:
Create the property files in the /WEB-INF/classes/resources directory with a .properties extention. Now you can add the properties your application will use in each file:

application.properties
site.title=Site Title

registration.properties
username=Your Username

JSP:
Now you can use the properties from either the default bundle or the registration bundle by specifying the bundle (from the key in the struts-config).

<bean:message bundle="registration" key="subhead.note" />
<bean:message key="errors.general" />

You can add more resource bundles by defining them in the struts-config file with a unique key.

No comments: