Pregunta

I am migrating an application from Struts 1.1 to Struts 2.3. The existing application uses MessageResources from the older framework in the following manner.

<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.

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. 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.

¿Fue útil?

Solución

If you want to search in particular file only then you can use <s:i18n> tag.

Using getText method if your action extends ActionSupport

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

or using <s:text> tag

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

Otros consejos

Use the i18n tag

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.

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.

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>

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top