This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 customized Exception handler

 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://struts.apache.org/2.x/docs/writing-interceptors.html

Specifically, PreResultListener.
 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks for prompt reply.
I am trying now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic