Hi all,

I finally had success in displaying messages in my jsp page, and wanted to
share this because it was kinda hard.

(disclaimer, I may not being using ActionMessages in the right way, but I like
the way it worked out, so I don't care).

problem: When the user of my little study schedule program adds or deletes a
new course to her study schedule, I want to give her a message in the next jsp
page, telling her how many courses she has added/deleted. I don't want the
message to print out as a big ugly red "ERROR", but rather as a bullet list of
messages. Therefore, I want to use ActionMessages, not ActionErrors. As I
interpret ActionMessages, I can use them to display results of actions that
aren't necessarily errors.

Here is what I did:

In my SaveScheduleAction, after courses are added or deleted from my database,
I do this:

          String rowsadded = sslogic.getRowsAdded().toString();
          String rowsdeleted = sslogic.getRowsDeleted().toString();
          amessages.add("rowsadded", new ActionMessage("message.rows.added",
rowsadded)); 
          amessages.add("rowsdeleted", new ActionMessage("message.rows.deleted",
rowsdeleted)); 

Then, in SaveScheduleAction, I save all my ActionMessages: 

        // Report any messages we have discovered back to the original form
        if (!amessages.empty()) {
            saveMessages(request, amessages);
              System.out.println("saved " + amessages.size() + "messages in the
request");
        }

Here is how my messages look in ApplicationResources.properties:

message.rows.added=Du har tilføjet {0} ny kursus
message.rows.deleted=Du har bortvist {0} kursus

And finally, and this was the tricky part, I had to get the messages to 
print in my jsp page. I borrowed from Ted Husted's very informative tip
on how to optimize error printing in jsp: "Don't settle for <html:error/>" at
http://husted.com/struts/tips/017.html

This is what I have in viewschedule.jsp:

<logic:messagesPresent message="true"> 
there were some messages
<UL>
 <html:messages id="message" message="true"/>
<LI><bean:write name="message"/></LI>
 </html:messages> 
</UL> 
</logic:messagesPresent>

As you can see in the above, there are id="message" and name="message". I'm
not positive, but I think that one of those ="message" refers to the
"message" in 

message.rows.added=Du har tilføjet {0} ny kursus
message.rows.deleted=Du har bortvist {0} kursus

from my ApplicationResources.properties, so it may be important to prefix
any strings that you want to display as messages, with "message."

Hope this helps someone!

Heather Buch

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

Reply via email to