On 6/7/06, Chris Cheshire <[EMAIL PROTECTED]> wrote:
In my actions I have messages stored in the session either under
ActionMessages.GLOBAL_MESSAGE, or my own key (eg 'search').

In my jsp I have a section to display the search specific messages.
<logic:messagesPresent message="true" property="search">
....
</logic:messagesPresent>

I also need to display the global messages at the top of the page,
however this displays everything, including the search messages:

<logic:messagesPresent message="true">
...
</logic:messagesPresent>

What do I set for the property to ONLY display the messages keyed by
ActionMessages.GLOBAL_MESSAGE?

You can do this in different ways:

1) Store the two different sets of messages in different
ActionMessages objects under different keys in the request or session.

So for example in your action you could store the search messages
using a session attribute key of "search-messages" - in your Action
you might have:

 ActionMessages searchMsgs = new ActionMessages();
 searchMsgs.add("fooSearch", new ActionMessage("foo.search.key"));
 searchMsgs.add("barSearch", new ActionMessage("bar.search.key"));
 session.setAttribute("search-messages", searchMsgs);

Then to display these messages, you use the "name" attribute

 <logic:messagesPresent name="search-messages">
     <html:messages name="search-messages" id="...">
         ....
     </html:messages>
 </logic:messagesPresent>

You could then store your "global" messages under the default Struts
"messages" key - so in your Action you might have

 ActionMessages globalMsgs = new ActionMessages();
 globalMsgs.add("fooGlobal", new ActionMessage("foo.global.key"));
 globalMsgs.add("barGlobal", new ActionMessage("bar.global.key"));
 saveMessages(session, globalMsgs);

Then to display these messages, you use the message="true" attribute

 <logic:messagesPresent message="true">
     <html:messages message="true" id="...">
         ....
     </html:messages>
 </logic:messagesPresent>

This saveMesages() method and message="true" attribute are just a
convenience feature so that you don't have to specify a key in the
request/session to save them under and to display them.

2) The second option is to save both sets of messages in one
ActionMessages object and use the "property" attribute to filter the
two different types. So add search messages with a property of
"search" and global messages with a property of "global":

 ActionMessages msgs = new ActionMessages();
 msgs.add("search", new ActionMessage("foo.search.key"));
 msgs.add("search", new ActionMessage("bar.search.key"));
 msgs.add("global", new ActionMessage("foo.global.key"));
 msgs.add("global", new ActionMessage("bar.global.key"));
 saveMessages(session, msgs);

To Show the global messages:

 <logic:messagesPresent message="true" property="global">
     <html:messages message="true" property="global" id="...">
         ....
     </html:messages>
 </logic:messagesPresent>

To Show the search messages:

 <logic:messagesPresent message="true" property="search">
     <html:messages message="true" property="search" id="...">
         ....
     </html:messages>
 </logic:messagesPresent>

The limitation on this approach is that you need to use the same
property name for the messages of a specified type.

I put up some notes on messages a while back here:
http://www.niallp.pwp.blueyonder.co.uk/HelpTagsErrorsAndMessages.html

Niall

Thanks,

Chris

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

Reply via email to