Mikaveli Mikaveli - 1 year ago 102
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

element - separating the messages into files for specific modules of the application.

The official Apache documentation states:

You can define one or more
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:

<message-resources parameter="resources.DefaultResource"/>
<message-resources parameter="resources.Registration"/>

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

Many thanks.

Answer Source

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 :

    <message-resources parameter="resources.DefaultResource"/>
    <message-resources parameter="resources.Registration" key="registrationBundle"/>    

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"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download