4

我正在使用(学习...)Struts 1.3 来构建 MVC Web 应用程序。为清楚起见,我想包含多个<message-resources>元素 - 将消息分成应用程序特定模块的文件。

Apache官方文档指出:

你可以为你的 webapp 定义一个或多个<message-resources>元素;模块可以定义自己的资源包。在您的应用程序中可以同时使用不同的捆绑包,'key' 属性用于指定所需的捆绑包。

但是,当我包含多个元素时,JSP 会导致异常,指出缺少 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~~~

这是 XML:

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

如果没有第二个“注册”资源,则不会引发异常。“label.username”仅存在于“DefaultResource”中。

非常感谢。

4

3 回答 3

11

使用这个 struts-config,第二个消息资源元素使用与第一个相同的(默认)键,因此完全替换了第一个。您必须为每个捆绑包分配不同的键,并使用 bean:message 标记中的捆绑包属性来指示您要使用哪个捆绑包:

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

在 JSP 中:

Message from the default bundle : <bean:message key="my.first.key"/>
Message from the registration bundle : <bean:message key="my.second.key" bundle="registrationBundle"/>
于 2011-01-21T17:02:16.640 回答
2

我相信您需要提供 key 属性。该键应在 jsp 中的标记中使用,以显示来自资源属性文件的特定消息。看看这个教程

于 2011-01-21T17:01:18.353 回答
2

答案就在您问题中包含的文档片段中。如果您有多个捆绑包,

在您的应用程序中可以同时使用不同的捆绑包,'key' 属性用于指定所需的捆绑包。

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

在您的 struts-config.xml 文件中包括一个键属性(具有唯一值)以及参数属性。如果没有不同的键,resources.Registration 将覆盖 resources.DefaultResource
(为了测试这个假设,请在 struts-config 中切换两个消息资源的顺序。然后,您的 label.username 将起作用,但来自另一个捆绑包的消息将不起作用)

于 2011-01-21T17:02:11.020 回答