How to include multiple message resources in Struts?

21,554

Solution 1

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"/>

Solution 2

The answer is right there in the documentation snippet you included in your question. if you have more than one bundle,

Different bundles can be used simultaneously in your application, the 'key' attribute is used to specify the desired bundle.

http://struts.apache.org/1.3.10/struts-core/dtddoc/struts-config_1_3.dtd.html#message-resources

Include a key attribute (with unique values) along with the parameter attribute in your struts-config. without a distinct key, resources.Registration is overwriting resources.DefaultResource
(To test this assumption, switch the ordering of two message-resources in struts-config. Then, your label.username will work but messages from the other bundle won't)

Solution 3

I believe you need to provide the key attribute. The key should be used in tag in jsp to display particular message from the resource property file. Take a look at this tutorial.

Share:
21,554
Michael
Author by

Michael

I'm a software engineer, especially interested in web applications, Java and Python development, Linux-based distros, DevSecOps, automation, containers and CNI. I contribute towards a small number of Open Source projects, balancing out my karma. :D

Updated on October 12, 2020

Comments

  • Michael
    Michael over 3 years

    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.