Dynamic key in bean:message in Struts 1.3

25,369

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"/>
Share:
25,369
Leonardo
Author by

Leonardo

Updated on May 05, 2020

Comments

  • Leonardo
    Leonardo about 4 years

    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

  • Leonardo
    Leonardo about 12 years
    I also found out that <bean:message key="<%= sample %>"/> solve the issue, but your solution is much more cleaner and Struts prone, and it works too.
  • Olivier Tonglet
    Olivier Tonglet over 7 years
    The correct alternative syntax would be <bean:message key="employeuronss.eid.warning" arg0="<%=sample%>" />