File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Handling Messages (v1.2.9) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Handling Messages (v1.2.9)" Watch "Handling Messages (v1.2.9)" New topic
Author

Handling Messages (v1.2.9)

Scott Arnold
Greenhorn

Joined: Oct 24, 2007
Posts: 6
I'm having trouble with figuring out how to handle messages in Struts.

For the sake of experimentation, here is what I have in my action method:



and in my jsp file:



When I execute the action, the only thing that shows up is the text "Messages present".

I know my message resources file is being found because I am using Struts Validation and the validation messages show up under the html:errors when I enter an error in the form. This also tells me that Struts is finding the tag library okay as well. And yes, I have messages.saved, messages.test, and messages.amg all defined in the message resources file.

What is the proper way to handle adding error and informational messages? And what might my problem be?

Note: I am using tiles. Could this be complicating matters?

Thanks,

Scott

Update: I also tried using message literals rather than keys to the message resources bundle. Same result.

i.e.:


[ October 31, 2007: Message edited by: Scott Arnold ]

[ October 31, 2007: Message edited by: Scott Arnold ]

[ October 31, 2007: Message edited by: Scott Arnold ]
[ October 31, 2007: Message edited by: Scott Arnold ]
Scott Arnold
Greenhorn

Joined: Oct 24, 2007
Posts: 6
As an additional diagnostic, I tried doing a saveMessages(request.getSession(),messages) with no change in results.

Why isn't there better information online for how to make use of Struts ActionMessages? And why isn't it more intuitive? I'm really starting to get aggravated. Using ActionMessages should be easy and clearly explained online in many places. Yet it is apparently not. Now I have to either abandon it or start digging into Struts source code to figure out what should be a simple part of the framework. Ridiculous.
Scott Arnold
Greenhorn

Joined: Oct 24, 2007
Posts: 6
Some additional diagnostics -- I tried simply adding the messages to the request manually using my own key as follows:



And in the jsp:



This time the messages showed up when used with html:errors but not under html:messages.
Scott Arnold
Greenhorn

Joined: Oct 24, 2007
Posts: 6
Okay, I've finally figured it out I think. I was missing the include for the bean tag library. I don't suppose there is a log file somewhere that would have indicated that there were unrecognized tags?

Best I can tell now that I can actually get it to work and play with it, it looks like first parameter for the add method of the ActionMessages class ties the error to a particular property (a property typically being a single field on a form, but could probably be anything). Therefore, if the message doesn't need to be tied to a particular field, some global constant should be used, which is provided through the constant ActionMessages.GLOBAL_MESSAGE. From there, whether it gets recognized as a message message or an error message is determined by whether you call saveMessages or saveErrors.

Therefore, to save a generic informational message, the proper code would be as follows:



And in the jsp, they could be displayed using:



The code is kind of excessive. It would be better if they had a method such as addMessage(ActionMessage actionMessage) and addMessage(String property, ActionMessage actionMessage). Then you could just call addMessage(new ActionMessage("some.key")).

To anyone who is confused about how the above example would be done for errors, it would look like this (assuming I am correct now):



with the jsp like this:



Hope this thread helps someone someday...
[ October 31, 2007: Message edited by: Scott Arnold ]
 
Consider Paul's rocket mass heater.
 
subject: Handling Messages (v1.2.9)
 
Similar Threads
Not able to display ActionMessges
ActionMessages?????????
Re, Handling Error using ActionMessages
Error Message Not Displaying on JSP(URGENT)
Issues with <html:messages >