aspose file tools*
The moose likes Struts and the fly likes jsp access exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "jsp access exception" Watch "jsp access exception" New topic
Author

jsp access exception

rob harvey
Greenhorn

Joined: Apr 05, 2002
Posts: 25
hi - with struts can I actaully access the exception that was thrown from the action in the jsp? ie I want to access one of the exceptions fields to retrieve some information to display to the user.
Zip Ped
Ranch Hand

Joined: Jul 26, 2005
Posts: 336
Store the exception as a request or session attribute and access the attributes in your jsp.
rob harvey
Greenhorn

Joined: Apr 05, 2002
Posts: 25
does it not get stored automatically? i dont want to handle it in my action - it is declared in the config file and thrown from the service layer
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Store the exception as a request or session attribute and access the attributes in your jsp.


Its not the right way of handling exception. Is it some transfer object which you are adding it in request or session scope.

Use struts declarative exception handling mechanism of to handle the exception.

Read this article...

Exception Handling

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
rob harvey
Greenhorn

Joined: Apr 05, 2002
Posts: 25
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.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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.

Here is my TestException class...



2)
Here is execute() method of Action class...



3)
error.jsp page




Naseem
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's what I'd suggest:

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.


Merrill
Consultant, Sima Solutions
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi Merrill,

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

Naseem
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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:

<bean:write name="theException" property="message" />

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 ]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Yes Exactly. Thanks Merrill for your suggestions.

Regards

Naseem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp access exception