Author Login
Post Reply
1. In struts.xml, use global-exception-mappings to define mappings that
catch-all exceptions of interest.
<global-exception-mappings>
<exception-mapping exception="java.lang.NoSuchMethodException"
result="methodNotSupported"/>
<exception-mapping exception="java.lang.IllegalArgumentException"
result="methodNotSupported"/>
<exception-mapping exception="java.lang.Throwable" result="error"/>
</global-exception-mappings>
2. In struts.xml, define global-results that are referenced by the
global-exception-mappings. These are standard struts2 results that can
render your error page.
Here's an example:
<global-results>
<result name="methodNotSupported" type="httpheader">
<param name="error">405</param>
<param name="errorMessage">The requested method is
unsupported</param>
</result>
<result name="error" type="redirect">/error.jsp</result>
</global-results>
3. The interceptor cannot catch exceptions that occur outside the action
invocation. As you've already discovered, you have to use the
container's error handling for these.
If you're using Apache in front of tomcat you'll also need to repeat the
information a third time there (eg. error page if tomcat is down).
Hope that helps,
Jeromy Evans
Jakub Milkiewicz wrote:
> Hi
>
> i would like to know what is the best practice to handle exceptions in
> Struts based applications.
>
> I have defined ExceptionMappingInterceptor aka exception interceptor and it
> works but it can not handle all kind of exceptions.
> I would like to have various exceptions that can occur in various parts of
> my system handled in an uniform manner and
> and my only requirements are: have these exceptions logged and present user
> simple error page.
>
> ExceptionMappingInterceptor is not able to satisfy my requirements in
> these 2 cases:
>
> 1) If no action with given URL is defined in struts.xml i see on a
> screen *:There
> is no Action mapped for namespace / and action name {actionName}. - [unknown
> location]
> and stacktrace that begins with :
> *
>
> com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
> org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
> org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
> org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
> com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
>
> Full stacktrace is logged.by my logger but http response code is 200
>
> 2) I am using Spring plugin and my actions are spring managed bean. If there
> is any spring related error in my actions (spring beans) i got an error
> message on screen,
> Http response is 500 now and i see stacktrace in my SystemOut.log :
>
> at
> com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
> at
> com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:365)
> at
> com.opensymphony.xwork2.DefaultActionInvocation.access$000(DefaultActionInvocation.java:38)
> at
> com.opensymphony.xwork2.DefaultActionInvocation$1.doProfiling(DefaultActionInvocation.java:83)
> at
> com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
> at
> com.opensymphony.xwork2.DefaultActionInvocation.<init>(DefaultActionInvocation.java:74)
> at
> com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)
> at
> org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
> at
> org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
> at
> org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> at
> com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
> at
> com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
>
>
>
> I would like to have various exceptions that can occur in various parts of
> my system handled in an uniform manner.
>
> What is the best practice to handle all these exceptions?
>
> Up to now i have defined ExceptionMappingInterceptor interceptor and it
> satisfies all my requirements but it only works when exceptions are thrown
> from actions.
>
> In case 2 (spring managed action classes) i can define error-page element
> in web.xml to handle errors with code 500 and have it redirected to the same
> page as defined in ExceptionMappingInterceptor interceptor.
> This configuration does not catch exception mentioned in case 1, but also
> has small drawback: address of error page to present user is duplicated in
> struts.xml and web.xml.
>
> Can someone provide any ideas/hints/explanations?
>
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG.
> Version: 7.5.524 / Virus Database: 269.23.16/1431 - Release Date: 13/05/2008 7:55 PM
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)