Dimitris Mouchritsas wrote:
Antonio Petrelli wrote:
2008/6/16 Dimitris Mouchritsas <[EMAIL PROTECTED]>:
Hi all,
we're using struts 1.2.4 in our project and we have 3 types of messages to
show the user:

1) Confirmational, with say a green tick mark,
2) Informational, with a classic i in a bubble
3) Error messages, with the classes red rectangle.

Currently I use saveErrors and saveMessages in my actions but that gives me
only 2 kinds of messages;
errors and all others. Is there a way to add a 3rd type? I think I should do
it by

errors.add("My.Custom.Identifier", ....);

but I'm not sure.

Well, this is the right thing to do :-)
In fact, saveErrors and saveMessages put the messages using special keys:
Globals.ERROR_KEY:
http://struts.apache.org/1.2.4/api/org/apache/struts/Globals.html#ERROR_KEY
Globals.MESSAGE_KEY:
http://struts.apache.org/1.2.4/api/org/apache/struts/Globals.html#MESSAGE_KEY

HTH
Antonio
Ok, I've succeeded, but in trying to have a uniform method for all messages I lost struts validator's errors.
For example, in the action:

       ActionMessages msg = new ActionMessages();

msg.add(MyConstants.MESSAGE_CONFIRM, new ActionMessage("prompt.OK")); msg.add(MyConstants.MESSAGE_INFO, new ActionMessage("prompt.modify")); msg.add(MyConstants.MESSAGE_ERROR, new ActionMessage("prompt.close"));

       saveMessages(request, msg);

then in the jsp, we have a tile to show all messages, regardless of where they come from:

<logic:messagesPresent message="true" property="<%=MyConstants.MESSAGE_CONFIRM%>">
   <div id="Message" class="Success">
       <%--<p><strong>Your changes have been accepted</strong>.</p>--%>
<html:messages id="msg" message="true" property="<%=MyConstants.MESSAGE_CONFIRM%>">
           <p>
               <bean:write name="msg" />
           </p>              </html:messages>
       <p class="Buttons">
<button id="MClose" type="submit"><bean:message key="prompt.close" /></button>
       </p>
   </div><!-- Message // -->
</logic:messagesPresent>


<%-- Informational Messages --%>
<logic:messagesPresent message="true" property="<%=MyConstants.MESSAGE_INFO%>">
   <div id="Message" class="Info">

<html:messages id="msg" message="true" property="<%=MyConstants.MESSAGE_INFO%>">
           <p><bean:write name="msg" /></p>              </html:messages>
             <p class="Buttons">
<button id="MClose" type="submit"><bean:message key="prompt.close" /></button>
       </p>
   </div><!-- Message // -->
</logic:messagesPresent>

<%-- Error messages --%>
<logic:messagesPresent message="true" property="<%=MyConstants.MESSAGE_ERROR%>">
   <div id="Message" class="Error">
       <p><strong><bean:message key="error.occured" />:</strong></p>
       <ul>
<html:messages id="msg" message="true" property="<%=MyConstants.MESSAGE_ERROR%>">
               <li>
                   <bean:write name="msg" />
               </li>
           </html:messages>
           <%--
           <li><a href="#FName">First name</a> is required.</li>
           <li><a href="#LName">Last name</a> is required.</li>
           --%>
       </ul>                  <p class="Buttons">
<button id="MClose" type="submit"><bean:message key="prompt.close" /></button>
       </p>
   </div><!-- Message // -->
</logic:messagesPresent>

But by using this method, the errors that validator adds, are not displayed, because they're save under Globals.ERROR_KEY. Is there a way to tell validator to save its errors as a message under my own custom key? Again I point out that the only reason I want this is to have a uniform way of dealing with messages, not having to use saveErrors. But if this is not correct I guess I'll
revert into using saveErrors. Any suggestions?

Regards
Dimitris

For each different type of messages store the corresponding ActionMessages object under a different key as you have done. However, leave the normal messages and errors alone. Globals.ERRORS is where the validator will store it's messages. This is also how struts determines if validation fails or not (is the ActionMessages under Globals.ERRORS empty or are there messages to display) and this is why your validation isn't working.

I strong suggest you leave the normal struts messaging alone (Errors & Messages) and just add the new message groups that you want under your own variables.

Caution:
I'm not entirely sure, but you may have to be more explicit in your message displaying than using <logic:messagesPresent>. This is because that tag will look under Global.MESSAGES to find the messages to display. The property="" that you're listing is used to look within that ActionMessages object for values that are stored for that property within the action messages collection. The variable you're supplying in your msg.add(VARIABLE, ActionMessage()); calls is the 'property' that the message is for.
Example:
If the *username* field has a validation error, validator would put a
msgs.add("username", new ActionMessage("Validation failure message"));
like so, and then put the msgs object under the Globals.ERRORS key.

To store your own messages you'll have to do something like this:
{
      ActionMessages msgs = new ActionMessages(); // Normal messages
      ActionMessages errors = new ActionMessages(); // Normal errors
ActionMessages confirms = new ActionMessages(); // My custom messages collection

confirms.add("username", new ActionMessage("prompt.confirm.username"));
      msgs.add("firstname",    new ActionMessage("prompt.modify"));
errors.add("postal_code", new ActionMessage("prompt.badpostalcode"));

saveMessages(request, msgs); // This concatenates with the Messages in struts Globals.MESSAGES saveErrors(request, errors); // This concatenates with the Messages in struts Globals.ERRORS // You may want a helper method to do this, so it can also check if you've already saved some messages
      request.setAttribute(MyConstants.MESSAGE_CONFIRM, confirms);

} Now keep in mind too, that the struts saveMessages method does a bit more than just store the messages object to the correct variable. What it actually does is checks if there's already an existing ActionMessages object there, and if so it will add your new messages to it. I strongly recommend you download the source for your version and see how it works, then you can build yourself a corresponding helper for your own message category.

ihth,

Louis

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

Reply via email to