Struts 2 - how to pass exceptions globally to a si

2019-08-30 09:35发布

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条回答
地球回转人心会变
2楼-- · 2019-08-30 10:29

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" />
查看更多
登录 后发表回答