Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem displaying exceptions

 
Bryan Comstock
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm in the process of implementing a declarative exception handler for my Struts web app. The code passes the exception to the handler properly and finds exception.jsp properly, but I can't get my messages to appear on exception.jsp. Here is what my configuration looks like:

<global-exceptions>
<exception
type="java.lang.Throwable"
key="exception.details"
handler="pkg.exceptions.ExceptionHandler"
path="/exception.jsp"/>
</global-exceptions>

I have a key in my MessageResources.properties file called 'exception.details' and it's value is set to {0} or just whatever message is passed in. In my exception handler, I'm calling storeException() as such:

storeException(request, "exception.details", new ActionMessage(ex.getMessage()), new ActionForward(ae.getPath()), ae.getScope());

I can see the exception message being stored in the request attributes just after the storeException() method call, so I know it's there.

I've tried numerous things to get the message to display on exception.jsp, but nothing works.

Any ideas? Thanks.
[ March 01, 2007: Message edited by: Bryan Comstock ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just let the default Struts code in the ExceptionHandler superclass handle the error message? I'd suggest just doing whatever logging or extra activities you need to do in your execute method, and then just end the method with:

This will handle putting the error message in either the request or the session and passing the exception message as the first parameter

Then in your JSP, just display the errors with either a <html:errors /> tag or a combination of <html:messages> and <bean:write> tags.
 
Bryan Comstock
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions Merrill. I may end up switching back to allowing Struts to handle it..

My main issue is actually getting the error message to appear on my page, though. I've tried using <html:errors/> and <html:messages>, but neither of them show the message.

If I use <html:messages> does the property need to be the key that I defined or some global key?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of both <html:errors> and <html:messages> you should not specify a property attribute at all. This will display all available messages, which is what you want.

If <html:errors> didn't work, that may be an indication that you didn't set the message properly. Try letting Struts set the message as described above, and see if that fixes it.
[ March 01, 2007: Message edited by: Merrill Higginson ]
 
Bryan Comstock
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks to your help I got the messages displaying now. I changed my exception handler to just call super.execute() and then both <html:errors/> and <html:messages> (without a property) displayed the messages.

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic