Multiple Message Resources in Struts…
In one way, Struts does support multiple resource bundles. to
many of us though, the default Struts mechanism of using
multiple resource bundles is not sufficient. Here is a brief
introduction to the default Struts mechanism for using multiple
resource bundles.
- You
can define multiple message resource file configurations in the
struts-config.xml in the following way:
<message-resources key="first" parameter="a.properties"/>
<message-resources key="second" parameter="b.properties"/>
-
In
order to use multiple resources from defined bundle, within the JSP you
have to qualify the <bean:message> tag with the name of the bundle.
For example, if you want to access a resource from the b.properties
bundle, then the JSP will look like this:
<bean:message bundle="second" key="some.message.key"/>
Here, you are pointing to the right resource bundle by mapping
against the specified "key" in the configuration file.
This method presents the following problems:
- Moving
resources between different resource bundle means you have to change the
JSPs to point to the right resource bundle.
- This
approach needs to be decided before you advance in your development, as
revisiting all the JSPs to conform to this style of using multiple
resource bundles can be impossible for a large scale and distributed
projects.
- If
you are using Commons Validator with Struts, it is reported that Validator
framework does not work with multiple resource bundle files.
The solution is then to define as many resource files as you
like and to consolidate them before the Struts initializes and
caches them in a local memory storage. Again, it comes out that
the right point to do that is the initialization time and it is
evident that we have to override the Struts ActionServlet class
in order to do what we want to do.
By analyzing, how Struts stores and retrieves the values against
a particular resource bundle key, we discovered the following:
- Struts
uses a class called MessageResources which is responsible for loading any
specified resource bundle file and parsing through it before returning the
value for a given message key.
- When
you specify multiple resource files the way we explained before, the
Struts loads all the resource files but stores them under different
keys. That is why you need to specify the key in the JSP when we attempt
to retrieve the value for a message key.
What we really need to do is to parse all the specified resource
files at one go and store all the resources under one global
key. In order to do that, we decided to come up with our own
CustomMessageResources class (Listing 2), which will accept a
list of message resource files, parse all of them and store them
in an internal cache. When a message key is passed onto it, it
will look for the value in the internal cache store and return
the appropriate value stored against the specified key.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|