You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Niall Pemberton <ni...@gmail.com> on 2006/03/16 14:03:07 UTC

Re: Common Resource bundles across web projects, ActionMessage and html:messages

When you use messages from an alternate (i.e. non default) message
resources you need to specify the "bundle" attribute in the
<html:messages> tag.

So if you have something like this in your struts-config,xml:

  <message-resources parameter="ApplicationResources"/>
  <message-resources parameter="'GeneralMessages" key="common" />

Then to use your "'GeneralMessages" in your jsp you specify bundle="common":

    <html:messages id="msg" message="true" bundle="common">
         <bean:write name="msg" /><br/>
    </html:messages>

I notice that you are storing your messages using a property of
"Globals.MESSAGE_KEY" - often people get confused between the
"property" the message is associated with in the ActionMessages object
and the request attribute under which that ActionMessages object is
stored. Globals.MESSAGE_KEY is usually used as the request attribute
key - rather than the propery a message is associated with in
ActionMessages.

You may be wanting to store messages from different bundles in the
same ActionMessages object. The only way to do this would be to store
them under different "property" names in the ActionMessages and then
use that property in the jsp tags to "filter" messages for a specific
resources bundle:

   ActionMessages messages = new ActionMessages();
   messages.add("defaultFoo", new ActionMessage(...);
   messages.add("commonBar", new ActionMessage(...);
   saveMessages(request, messages);

then in your jsp:

    <html:messages id="msg" message="true"
            property="defaultFoo">
         <bean:write name="msg" /><br/>
    </html:messages>

    <html:messages id="msg" message="true" bundle="common"
            property="commonBar">
         <bean:write name="msg" /><br/>
    </html:messages>


This is however a misuse of what property was designed for and it
would be better to store messages from different bundles in different
ActionMessages objects

   ActionMessages defaultMsgs = new ActionMessages();
   defaultMsgs.add("foo", new ActionMessage(...);
   saveMessages(request, defaultMsgs);


   ActionMessages commonMsgs = new ActionMessages();
   commonMsgs.add("bar", new ActionMessage(...);
   request.setAttribute("commonMsgs", commonMsgs);

...then in your jsp

    <html:messages id="msg" message="true">
         <bean:write name="msg" /><br/>
    </html:messages>

    <html:messages id="msg" name="commonMsgs" bundle="common"
            property="commonBar">
         <bean:write name="msg" /><br/>
    </html:messages>

More info here

http://www.niallp.pwp.blueyonder.co.uk/HelpTagsErrorsAndMessages.html

Niall


On 3/16/06, gudny.hauknes@comptel.com <gu...@comptel.com> wrote:
>
> Hi all,
> We are using the ActionMessage-s with replacement values.
>
> We want to divide the resource bundles so that we have a 'common-layout-jar'
> which contains common messages in 'GeneralMessages.properties' and so the
> application war itself contains *all application specific messages* in
> 'ApplicationResources.properties', the last one set up as the default bundle
> in struts-config.xml.
>
> Generally, it seems that resource bundle texts from the default bundle
> *only* are accessible for an ActionMessage contructor by their key - (new
> ActionMessage(String key, Object o);)
>
> Therefore, from our experience, all bundle texts with *replacement objects*
> specifically,  must be placed in the *default bundle*.
>
> If not, the ActionMessage contructor with key (or possibly the
> <html:messages...> tag) and replacement values does not play - and an
> JspException is generated, see below.
>
> Does anyone have a solution to this, we want to refer *directly* to message
> keys in common resource bundles instead of having to concatenating strings
> manually. (That is the workaround we found to work if we insist on having
> the common messages once and for all defined in the common layout jar.)
>
> Thanks!
>
> Regards,
> Gudny H.
>
> -----------------------------------------------------------------------------------
> Code example:
> In the Action-class:
>  ActionMessages messages = new ActionMessages();
>  ActionMessage("general.textWithOneReplacementValue",
> replacementValue);
>  messages.add(Globals.MESSAGE_KEY, msg);
>
> Message associated with the key in the resource bundle:
>    general.textWithOneReplacementValue= Some message with one replacement
> value: '{0}'
>
> The JSP code we used for outputting the messages is:
>    <html:messages id="msg" message="true">
>         <bean:write name="msg" /><br/>
>    </html:messages>
>
> If "general.textWithOneReplacementValue" property is placed in other than
> the default bundle, this JspException is generated:
>
> 2006-03-16 10:45:03,779 ERROR [[/nims-web-5.0-SNAPSHOT].[action]]
> Servlet.service() for servlet action threw exception
> javax.servlet.jsp.JspException: Cannot find bean: "msg" in any scope.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org