Struts中如何包含多个消息资源?
我正在使用(学习...)Struts 1.3 来构建 MVC Web 应用程序。为了清楚起见,我想包含多个
元素 - 将消息分隔到应用程序特定模块的文件中。
官方 Apache 文档 指出:
您可以为您的网络应用定义一个或多个
元素;模块可以定义自己的资源包。不同的包可以在您的应用程序中同时使用,'key'属性用于指定所需的包。
但是,当我包含多个元素时,JSP 会引发异常,指出缺少键消息:
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”中。
非常感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此 struts-config,第二个消息资源元素使用与第一个消息资源元素相同的(默认)密钥,从而完全替换第一个消息资源元素。您必须为每个捆绑包分配不同的密钥,并在 bean:message 标记中使用捆绑包属性来指示要使用哪个捆绑包:
并在 JSP 中使用:
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 :
and in the JSPs :
我相信您需要提供关键属性。该键应该用在 jsp 中的标记中,以显示资源属性文件中的特定消息。看看这个教程。
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.
答案就在您问题中包含的文档片段中。
如果您有多个捆绑包,
http://struts.apache.org/1.3.10/struts-core/dtddoc/struts-config_1_3.dtd.html #message-resources
在 struts-config 中包含一个键属性(具有唯一值)以及参数属性。如果没有不同的密钥,则 resources.Registration 会覆盖 resources.DefaultResource
(为了测试这个假设,请在 struts-config 中切换两个消息资源的顺序。然后,您的 label.username 将起作用,但来自其他包的消息将不起作用)
The answer is right there in the documentation snippet you included in your question.
if you have more than one 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)