Struts 2 - how to pass exceptions globally to a si

2019-08-30 10:01发布

问题:

I want to pass exceptions globally to a single action file called ErrorAction, say from Index action.

Here's my struts.xml file:

<global-results>    
    <result name="myErrorHandler" type="redirectAction">    
        <param name="actionName">myError</param>
    </result>    
    <result name="login" type="tiles">
        login
    </result>    
</global-results>    

<global-exception-mappings>    
    <exception-mapping exception="java.lang.Exception" result="myErrorHandler" />    
</global-exception-mappings>    

<action name="myError" class="com.actions.ErrorAction">    
    <interceptor-ref name="exception" />    
    <interceptor-ref name="defaultStack" />    
    <result name="error" type="tiles">error</result>    
</action>   

....    

<action name="Index" class="com.actions.Index">    
    <interceptor-ref name="defaultStack" />    
    <result name="success" type="tiles">home</result>    
</action>

回答1:

The exception interceptor is already included in the defaultStack. Use the custom stack so that any exceptions not caught by the application will be logged and then handled by the global exception mapping

<interceptors>
  <interceptor-stack name="appDefaultStack">
    <interceptor-ref name="defaultStack">
      <param name="exception.logEnabled">true</param>
      <param name="exception.logLevel">ERROR</param>
    </interceptor-ref>
  </interceptor-stack>
</interceptors>
<default-interceptor-ref name="appDefaultStack" />