I am displaying ActionMessages through a JSP file by the following command:
<logic:messagesPresent message="true">
<ul id="messsages">
<html:messages id="msg" message="true">
<li><bean:write name="msg"/> </li>
</html:messages>
</ul>
</logic:messagesPresent>
Now I want to display only selected messages. How can I indicate which message to display?
Updated
Actually I have two ActionMessages object – messages and warnings.
Now I want to display both of them on separate JSP page… One page to show messages and other for warnings.
So how to indicate in the JSP page that which messages to display?
Updated – 2
Now, I found a strange thing.
saveMessages(request, messages);
saveMessages(request, warnings);
When I wrote the above code, only warnings was working. When I reversed the order of the above two statements, then only messages was working.
It seems that we can add only one ActionMessages object in a request. If it is correct, then how to display messages in two ActionMessages objects seperately.
Simple,
Separate your
messagesand yourwarnings: In your struts action, save your messages and warnings as follows:To display them:
This displays all
messages(by settingmessage="true")This displays all
warnings(by settingmessage="false")UPDATE Seeing that you’re now clearing your question, the simplest way would be to do this.
Have a certain flag that will indicate whether the user would like to view
messagesorwarnings. On the Struts Action, request the flag and check if the user selected viewing messages or warnings.You then save either
warningsormessagesbased on the user selection and display the same page (as you wrote above) to display messages.The reason is this, Struts (when storing you messages or errors) stores it on request or session with the following constant.
saveMessages(request, messages))saveErrors(request, errors))when using
<logic:messagesPresent message="true">, Struts searches for theMESSAGE_KEY(if message=true) orERROR_KEY(if message=false) or both (if message=none). You have no control of that.<html:messages />TLD comments states:You can also write scriptlet to check if those keys exists, then
<logic:iterate />through the key to display the messages (but that’ll be too much work).Hope this helps.