简体   繁体   English

Struts 2消息资源包的替代

[英]Alternative to Struts 2 message resource bundle

I am migrating an application from Struts 1.1 to Struts 2.3 . 我正在将应用程序从Struts 1.1迁移到Struts 2.3 The existing application uses MessageResources from the older framework in the following manner. 现有应用程序通过以下方式使用旧框架中的MessageResources

<message-resources key="label" parameter="com.abc.xyz.resources.MyLabelResources"/>
<message-resources key="image" parameter="com.abc.xyz.resources.MyImageResources"/>

This way different resources files are separated out and whenever a look-up is done, the bundle key is specified so that it searches in that particular file only. 这样,可以分离出不同的资源文件,并且每当进行查找时,都会指定bundle键,以便它仅在该特定文件中搜索。

Based on my understanding on Struts 2.2 framework, I have concluded that I need a global properties file which will do the work for me. 根据对Struts 2.2框架的了解,我得出的结论是,我需要一个全局属性文件来为我完成工作。 However, I would want to create multiple global properties file say for labels and images like the way its working in the existing application (above). 但是,我想为标签和图像创建多个全局属性文件,例如其在现有应用程序中的工作方式。

I am not sure how I do this. 我不确定该怎么做。

If you want to search in particular file only then you can use <s:i18n> tag. 如果只想搜索特定文件,则可以使用<s:i18n>标记。

Using getText method if your action extends ActionSupport 如果您的操作扩展了ActionSupport请使用getText方法

<s:i18n name="labels">
  <s:property value="getText('some.label')" />
</s:i18n>

or using <s:text> tag 或使用<s:text>标记

<s:i18n name="labels">
  <s:text name="some.label" />
</s:i18n>

But you can also include more than one properties files to custom default resource bundles: 但是,您还可以在自定义默认资源包中包含多个属性文件:

<constant name="struts.custom.i18n.resources" value="images, labels" />

Use the i18n tag 使用i18n标签

Gets a resource bundle and place it on the value stack. 获取资源束并将其放在值堆栈上。 This allows the text tag to access messages from any bundle, and not just the bundle associated with the current action. 这允许text标签访问任何捆绑软件中的消息,而不仅仅是与当前操作关联的捆绑软件中的消息。

to load the resource bundle to the value stack and use it. 将资源束加载到值堆栈并使用它。


Like in this example. 就像在这个例子中一样。

Gets a resource bundle and place it on the value stack. 获取资源束并将其放在值堆栈上。 This allows the text tag to access messages from any bundle, and not just the bundle associated with the current action. 这允许text标签访问任何捆绑软件中的消息,而不仅仅是与当前操作关联的捆绑软件中的消息。

 name* - the resource bundle's name (eg foo/bar/customBundle) 

Example: 例:

  <s:i18n name="myCustomBundle"> The i18n value for key aaa.bbb.ccc in myCustomBundle is <s:property value="text('aaa.bbb.ccc')" /> </s:i18n> <s:i18n name="some.package.bundle" > <s:text name="some.key" /> </s:i18n> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM