Mikaveli Mikaveli - 1 month ago 8
Java Question

How to include multiple message resources in Struts?

I'm using (learning...) Struts 1.3 to build an MVC web application. For clarity, I'd like to include more than one

<message-resources>
element - separating the messages into files for specific modules of the application.

The official Apache documentation states:


You can define one or more
<message-resources>
elements for your webapp; modules can define their own resource bundles. Different bundles can be used simultaneously in your application, the 'key' attribute is used to specify the desired bundle.


However, when I include more than one element, JSP's cause an exception stating that there is a missing message for key:

SEVERE: Servlet.service() for servlet jsp threw exception javax.servlet.jsp.JspException: Missing message for key "label.username" in bundle "(default bundle)" for locale en_GB
at org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:233)
at org.apache.jsp.index_jsp._jspx_meth_bean_005fmessage_005f0(index_jsp.java:197)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:107) ~~~snip~~~


This is the XML:

<struts-config>
~~~snip~~~
<message-resources parameter="resources.DefaultResource"/>
<message-resources parameter="resources.Registration"/>
</struts-config>


Without the second "Registration" resource, the exception isn't thrown. "label.username" exists in the "DefaultResource" only.

Many thanks.

Answer

With this struts-config, the second message resources element uses the same (default) key as the first one, and thus replaces the first one completely. You must assign a different key to each of the bundle, and use the bundle atttribute in the bean:message tag to indicate which bundle you want to use :

<struts-config>
    ~~~snip~~~
    <message-resources parameter="resources.DefaultResource"/>
    <message-resources parameter="resources.Registration" key="registrationBundle"/>    
</struts-config>

and in the JSPs :

Message from the default bundle : <bean:message key="my.first.key"/>
Message from the registration bundle : <bean:message key="my.second.key" bundle="registrationBundle"/>