Derek Clarkson wrote:
Hi all,
I've just subscribed to this list and would appreciate any help you can give. I've been trying to get resources bundles to work. I have a jar file which is used on several projects and contains some core business beans. It also contains aproperty file with a basic set of messages for various errors.


In the project I am currently working on I have another properties file which contains more project specific messages. The problems is that I cannot find out how to access these messages in an Action.

Derek,


If my understanding is correct, neither ActionMessage nor ActionMessages do any resolution of the message so they are agnostic of the resource bundle the message is in. It's just a collection of keys until you try to resolve them on a JSP page. At that point you specify which bundle the message should be resolved from.


I've read a lot on the web, but most of the docouments talk about how you do it using jsp tags. In actions I can access messages using the ActionMessage class which are stored in the default properties file.

This is not actually corect. In actions, the ActionMessages object is just a collection of keys. Again, it's agnostic of which properties file the message comes from.


In my struts-config.xml though, I can only have one properties file as default and the other I have to provide a key for.

I can't find any way to tell ActionMessage what key to use to access the messages I want to use. I can do it using a MessageResources class, but it only provides the completed message Strings, and ActionMessage only accepts property keys.

I must be missing something, or there is a gap between these two ways of handle messages. All I want to do is (in an Action) retrieve a message from a specific properties file and display it in a document using <html:messages>. This shouldn't be this hard, any ideas ????

In your action class create and save the ActionMessages collection, then use the "bundle" attribute of the <html:messages> tag in your JSP to resolve the messages from a specific resource file.


I do see how it would be desireable for your case to be able to specify the bundle when you create the message. Then you could use one <html:messages> tag to display all your messages no matter what bundle they are in. But the Struts approach is to specify the bundle to resolve a message as close to the view as possible and let the rest of the Frameowrk not care.

Another option for you would be to store messages in multiple collections and use both the "name" and "bundle" attributes of the <html:messages> tag to find and resolve from a different bundle:

-----------------------------------------------------------------
In action:

ActionMessages myMessages = new ActionMessages();
myMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
    "foo.bar"));
request.setAttribute("myCollection", msgs);

ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
    "some.message"));
saveMessages(request, messages);

-----------------------------------------------------------------
In JSP:

<%-- To display messages in custom bundle --%>
<html:messages id="msg" name="myCollection" bundle="myBundle"/>
    <bean:write name="msg"/>
</html:messages>

<%-- To display messages in default bundle --%>
<html:messages id="msg">
    <bean:write name="msg"/>
</html:messages>

Greg


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to