This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
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...
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...
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
posted
0
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
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
posted
0
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?