Re. Handling Error using ActionMessages

Discussions

Web tier: servlets, JSP, Web frameworks: Re. Handling Error using ActionMessages

  1. Re. Handling Error using ActionMessages (4 messages)

    Hi, I am trying to store errors in struts using ActionMessages and display them in my jsp page. I get the following error: ERROR [org.apache.struts.taglib.tiles.InsertTag] - ServletException in '/genericError.jsp': Cannot find bean: "msg" in any scope My code snippet is as follows: ActionClass ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(ex.toString())); saveErrors(request, messages); genericError.jsp I checked some of the earlier postings and did not see anything that would help in my case. Any help is appreciated. Thanks, Peter
  2. Peter; Cannot find bean: "msg" in any scope... this error means, there is no object identified by "msg" found on either session scope. Inorder to get the snippet working properly, the method saveErrors(request, messages); method should be setting your 'messages' object with the identifier 'msg; in to request or session scope if I am correct. Another explanation is ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(ex.toString())); // this line probably threw an exception //and this got executed? or messages is on session scope.. saveErrors(request, messages); and you some how got in to the front end, and while your tag tried to extract/iterate messages, it coudnt find anything in the collection..
  3. Jyothish: Thank you for the sugestions. I had put breakpoints in my action class, and did notice that was being populated with the data. There was no exception thrown anywhere (even at 'saveErrors'). I did not read or hear anywhere about setting anything else other than what I have done in my code. Peter.
  4. Jyothish:

    Thank you for the sugestions.

    I had put breakpoints in my action class, and did notice that was being populated with the data. There was no exception thrown anywhere (even at 'saveErrors').

    I did not read or hear anywhere about setting anything else other than what I have done in my code.

    Peter.
    Peter, I think the way you are creating the ActionMessage in the action is the culprit. When you call "new ActionMessage(String)", it is assumed that the String parameter is a key to an entry in your message resources file. Struts default is to return null if not found in the resources so the html:messages tag may be hitting/swallowing a NullPointerException. Try changing your action to use "new ActionMessage(ex.toString(), false)". As a side note, if your struts-config has the following: null="false" /> Struts will return "???missing key???" instead of null for missing message keys.
  5. Just to clarify, I just looked at the Struts source for MessagesTag and it is not swallowing a NullPointerException but has: if (report.isResource()) { msg = TagUtils.getInstance().message(...); } else { msg = report.getKey(); } if (msg == null) { pageContext.removeAttribute(id); } else { pageContext.setAttribute(id, msg); } Thus using "new ActionMessage(ex.toString(), false)" should fix your problem. If you are using Struts 1.1.*, this constructor may not be available, but there is a workaround solution: Define the following message key in your resources file: errors.literal={0} In your action: new ActionMessage("errors.literal", ex.toString())