Dynamic key in bean:message in Struts 1.3

Leonardo picture Leonardo · May 4, 2012 · Viewed 24.1k times · Source

I am working on a project which made use of an old (but nice) framework Struts 1.3, and I am trying to accomplish a simple task.

I have a bean containing a property, which is a key corresponding to a property file. I would like to use it for recall the appropriate translation.

Variable in property file

props.myprop.sample=This is my sample property

The property is in a bean passed to the jsp called for convenience AllProps which has a getter for the property, and this should be a pseudo code:

<bean:define id="sample" name="AllProps" property="sample" type="java.lang.String"/> // should result in sample = props.myprop.sample

<div><bean:message key="sample"/></div>

Which should output:

<div>This is my sample property</div>

But obviously result in a property not found, can you give me help on how to deal with this ? I would like to stick as much as possible to Struts tag, then Jsp tag, and scriptlet as last resource.

Thanks

Answer

JB Nizet picture JB Nizet · May 4, 2012

Straight from the documentation:

<bean:message>

Render an internationalized message string to the response.

Retrieves an internationalized message for the specified locale, using the specified message key, and write it to the output stream. Up to five parametric replacements (such as "{0}") may be specified.

The message key may be specified directly, using the key attribute, or indirectly, using the name and property attributes to obtain it from a bean.

(emphasis mine)

So, all you need is

<bean:message name="AllProps" property="sample"/>