File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts 2 Exception Handling Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 Exception Handling Question" Watch "Struts 2 Exception Handling Question" New topic

Struts 2 Exception Handling Question

Chhaya Patil

Joined: Nov 14, 2008
Posts: 17

I am trying to use Struts2 exception handling mechanism.
Here is how my struts.xml file looks like for exception related entries -

<result name="Exception">/pages/error.jsp</result>

<exception-mapping exception="java.sql.SQLException" result="SQLException" />
<exception-mapping exception="java.lang.Exception" result="Exception" />

Here is my action related entry -
<action name="abc"
<interceptor-ref name="exception" />
<interceptor-ref name="abcStack" />

Now in my Action class when i get an exception i return "Exception" . ( I am not sure this is the way to do it, i am pretty new to Struts2.)
My error.jsp pages gets displayed but i do not see any exception message and stack trace.
I have <s:property value="%{exception.message}"/> , <s:property value="%{exceptionStack}"/> for displaying error msg and stack trace.

Does anyone have idea where i am getting wrong?

David Newton

Joined: Sep 29, 2008
Posts: 12617

I don't think *you* have to return "Exception", the only reason the exception result is named is so it can be referred to in the exception mappings: if the action throws the exception the framework handles mapping the exception to the result.

Also, if you map directly to a JSP I'm not sure that the <s:property> tags will work--if there's no action there may not be a value stack from which to retrieve the values. Try instead a "redirectAction", which can be a classless action mapping.
Chhaya Patil

Joined: Nov 14, 2008
Posts: 17
Thanks for replying.

the exception is from outside the execution of the action logic. it's from a data access class which i call from action. So i was getting 1 or 0 depending on success or failure from data access module. So if data acceess component had exception it was returning 1 and i was returning "Exception". which is wrong.

So now just to try, i copied the exception causing DB code into action itself and i am not returning "Exception". But when exception is occuring i see 500 error page but not Error.jsp with stack trace. So the entire exception trapping is not working. Any idea why?
It is sorta covered in the JavaRanch Style Guide.
subject: Struts 2 Exception Handling Question