ActionMessages, saveMessages and getResources oh my!

Back in the primordial ooze of Struts pre-1.1, people liked ActionErrors.
They started using them for tasks for which they were never intended. People
started using ActionErrors as a general-purpose way to display i18n-enabled
dynamic messages. Then Struts 1.1 added the concept of ActionMessages. If
you recall from our ActionErrors discussion in Chapter 10, "Managing
Errors," ActionErrors subclass ActionMessages. Now you can use
ActionMessages to display dynamic i18n-enabled messages without feeling like
a hack.

Working with ActionMessages is nearly identical to working with
ActionErrors. Here is an example of adding ActionMessages that you want to
be displayed inside an Actions Execute method:

    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        ActionMessages messages = new ActionMessages();



        ActionMessage message = new ActionMessage("inputForm.greet");
        messages.add(ActionMessages.GLOBAL_MESSAGE, message);

        ...
        saveMessages(request,messages);
        ...

Then to display the messages in the JSP, you use the html:messages tag as
follows:

<ul>
<font color='green' >
<html:messages id="message" message="true">
  <li><%= message %></li>
</html:messages>
</font>
</ul>

The html:messages tag will iterate over all of the messages. Notice that the
message attribute of html:messages is set to true. This forces html:messages
to get the messages from Globals.MESSAGE_KEY in request scope. If you do not
set the message attribute, the html:messages tag will display the errors
instead (Globals.ERROR_KEY).
What if you want to display an arbitrary number of messages—and it could
vary by locale? In that case, you use getResources to get the number of
messages for that locale:

    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        ActionMessages messages = new ActionMessages();



        ActionMessage message = new ActionMessage("inputForm.greet");
        messages.add(ActionMessages.GLOBAL_MESSAGE, message);

        String num =
            this.getResources(request).getMessage("inputForm.messageNum");

        int messageCount = Integer.parseInt(num);
        for(int index=0; index<messageCount; index++){
            String messageKey="inputForm.message" + index;
            message = new ActionMessage(messageKey);
            messages.add(ActionMessages.GLOBAL_MESSAGE, message);
        }
        saveMessages(request,messages);

        System.out.println(Globals.MESSAGE_KEY);
        if (messages ==
             request.getAttribute(Globals.MESSAGE_KEY)){
           System.out.println("its there can't you see it");
        }
       ...
The previous code corresponds to the following resource bundle entries:
inputForm.greet=Hello Welcome to Trivera Technologies
inputForm.messageNum=2
inputForm.message0=Please be sure to fill out your phone ...
inputForm.message1=Pick a user name and password you can remember


The inputForm.messageNum method specifies the number of messages for the
inputForm. The for loop iterates up to the count of messages, grabbing each
message for inputForm—that is, inputForm.message0, inputForm.message1, and
so on.
These examples cause the messages in the resource bundle to display in an
unordered list at the top of the input JSP page.
The RequestUtils class has a method called getActionMessages() that is used
by logic:messagePresent and html:messages. The getActionMessages method
allows you to retrieve the messages saved by Action.saveMessages and
Action.saveErrors.

Rick Hightower
Developer

Struts/J2EE training -- http://www.arc-mind.com/strutsCourse.htm

Struts/J2EE consulting --
http://www.arc-mind.com/consulting.htm#StrutsMentoring

-----Original Message-----
From: Otávio Augusto [mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 06, 2004 10:47 PM
To: [EMAIL PROTECTED]
Subject: messages


I'm able to send error messages to my jsp whenever needed. but how to i send
confirmation messages (for instance "Your mail has been sent") ? is there a
way similar to the ActionErrors method?

thanks


Otávio Augusto

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



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

Reply via email to