yes I know how to handle exceptions - I just want to know if I can access a property of one of the ones thrown in the jsp - ie ive declared it in the struts config - its thrown - then in the jsp i forward to i want to access it.
Joined: Apr 25, 2005
Well I come to a solution. I personally tried your case by writing a simple program. What I have done is I caught the exception in Action and gets its wrapped exception message, created ActionError sets the exception message in error, put that error in request scope and forward to an error.jsp page.
1-Write a Java class that extends org.apache.struts.action.ExceptionHandler
2-In your struts-config.xml file when you define a global exception, specify your class name in the "handler" attribute of the <exception> tag.
3-In your Exception Handler class, override the execute() method so that it takes the exception that is passed as a parameter and puts it in request scope. Make sure you call the original method first, though. Something like this:
4-In your JSP, you can then retrieve the exception from request scope and do whatever you want with it.
Is it correct to put exception objects in request scope? If its corect, then why we need a seperate handler for that. In Action itself, we can catch the same exception and put it in request scope and we will get the same result.
Thanks & Regards
Joined: Feb 15, 2005
The advantage to using the ExceptionHandler over simply catching the exception in the Action class is this:
One of the main reasons for using the Struts Global Exception feature is to avoid having to put try/catch blocks in every Action class. If we use an ExceptionHandler, we can still avoid try/catch blocks because the ExceptionHandler gets called by Struts when it catches an exception. If we catch the exception in every action class, we really don't need to use the global exception feature, because we've already caught the exception ourselves and can handle it. We don't need Struts to catch it for us.
Regarding your question about whether it is correct to put an Exception in the request: As far as I'm concerned, it's correct to put anything in the request that you want to put there. Generally, you want to put JavaBeans in the request, since most of the custom tags only work with JavaBeans. Even though the Exception class is not a JavaBean, the getMessage method follows the JavaBean convention, making it usable by a tag.
For example, if you implemented the code I showed above for the ExceptionHandler, there would be an Exception object in the request with the name "theException". In your JSP, you could display the exception's message with the code:
If you'd rather put an ActionMessages object into the session rather than the raw Exception class, you can still do that in the ExceptionHandler class if you want. [ July 25, 2006: Message edited by: Merrill Higginson ]