Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How best to access resource bundle text for input to Struts tags 1

Status
Not open for further replies.

jsmall

Programmer
Oct 26, 2004
4
0
0
US
I am currently using the <bean:message> tag to access string resources in a default resource bundle.

This works great for dispalying resource bundle text directly (e.g., <td><bean:message key="label.value"/></td>) but I also would like to access the resource bundle text as input to other tags. For example, I'd like to set the "value" attribute in an <html:text> tag from this bundle:

<html:text property="startAddrLine1" value="some default"/>

but I can't seem to figure out a direct method for doing this. Any ideas?

Thanks.

Jeff Small
jsmall@kivera.com

 
Probably the easiest way to do this is to get the string in you form handler and then access via a property

Code:
<html:text property="something.yourproperty" styleClass="fieldReg" maxlength="40"/

Then in your form have a getYourproperty() that retrieves the string from the message bundle. Something like this might do it

Code:
private String getYourproperty(){
    ResourceBundle rb = ResourceBundle.getBundle("ApplicationResources",request.getLocale());
    MessageFormat mf ;

    mf = new MessageFormat(rb.getString("some.message));
    return mf.format(args);
}

This assumes the 'request' is global in this scope, you may need to pass in the request object as well. Your mileage may vary :)

You can probably tweak it a lot to be generic to any string message name passed in etc. You could also pass in Object[] args to get more info. This little method is off the top of my head.



 
Thanks siberian. I actually found a better way which is to use the <bean:define> tag (and even better <c:set> from JSTL) to set a variable from the <bean:message> and then reference it with <%= %> (or ${ } if using <c:set>). For example:

<c:set var="myVal"><bean:message key="app.mVal"/></c:set>

<html:link action="/someaction"
styleClass="${myVal}">
<bean:message key="app.someOtherMessage"/>
</html:link>
 
Good tip and much more flexible since it can take args etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top