wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts2 customized Exception handler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 customized Exception handler" Watch "Struts2 customized Exception handler" New topic
Author

Struts2 customized Exception handler

Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Hi,
I have checked the official xwork website for customized exception handling, but it is too simple for my needs.
What I wanna do is having a customized exception message shown on the user browser,
and record the system exception (original exception message) to logger.

say, I have an exception of java.lang.NullPointerException from an action,
Struts2 interceptor catches this exception for me, and return the result name (say, "nullpointerr") on the global-exception-mappings in struts.xml

<global-exception-mappings>
<exception-mapping result="nullpointerr" exception="java.lang.NullPointerException">
</exception-mapping>
</global-exception-mappings>


<action name="checkaction" class="com.me.CheckAction">
<result name="nullpointerr">/index.jsp</result>
<result name="success">/index.jsp</result>
...
</action>

I want to record this nullpointer exception by logger, and return a customized message on index.jsp
Seems that I must override the java.lang.NullPointerException, and arrange the logger...

Please help

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Why are you mapping index.jsp as your error page result?? If you want to show page to the user, then map the error to some other page. If you want to add a line to index.jsp page, then you can use the exception and exceptionStack objects of the value stack (example here), you just need to use a few struts tags. There's no need to override NullPointerException. And NullPointerException is a JVM exception, I don't know how you are planning to override it and cause the JVM to throw your exception instead of NullPointerException...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The exception interceptor will already log the exception if it's set up properly. If that isn't enough, I'd probably just create a custom exception interceptor.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Thanks
Both global exception catch and customized interceptor do not fit my needs.
I have an action form which should provide warning/error messages on top of the page when it is something wrong on the db transaction.
The submission messages must be displayed on the same page (ie. index.jsp) for any types of errors.

I can catch the exception and throw it manually inside the action, but I don't like this approach.
try{
sth...
} catch (NullPointerException e){
throws new myException(e.getMessage());
}
Inside myException, the customized error message will be made and pass to index.jsp.
The default behavior of struts2 exception interceptor will stop the user from further submission, but display an exception page (dispatched to other screen).
Since I am using sitemesh, the user must be notified the problem and modify the form value allowing them to re-submit again.

I don't like the approach above just because I must catch exception for each action. Customized Interceptor is good to me, but it cannot dispatch to my designed result name (in struts.xml).

Please suggest me any better ways to solve this problems.
thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A customized interceptor can redirect you to whatever you want.

In any case, if, as you say, you want to stay on the "same page" without a redraw, then use Ajax.

I guess I just don't understand your requirement. I think you're handling something with exceptions that shouldn't be--business validation is still just that--validation. And if you *are* redrawing the screen, then showing a popup with JavaScript doesn't have anything to do with an interceptor. IMO nothing you call from an interceptor should throw an NPE; that should all be handled at lower levels, by services/etc.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Thanks

A customized interceptor can redirect you to whatever you want.
how?
What happen on this interceptor??


Since the exception is thrown, and it will display on screen, I just want to log this, and display a customized message to user screen.
How can I customized my error message using interceptor?

thanks

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/writing-interceptors.html

Specifically, PreResultListener.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42

thanks for prompt reply.
I am trying now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 customized Exception handler
 
Similar Threads
How to handle java.lang.NullPointerException in Struts2?..
migration to Struts2
Global Exception handling issue in Struts2
struts2 + interceptor
Custom result mapping in struts2