Struts 2 - how to pass exceptions globally to a single action file from other action files

◇◆丶佛笑我妖孽 提交于 2019-12-11 14:08:21

问题


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" />


来源:https://stackoverflow.com/questions/14751256/struts-2-how-to-pass-exceptions-globally-to-a-single-action-file-from-other-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!