ExceptionMappingInterceptor 总结

This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map an exception to a result code, just as if the action returned a result code instead of throwing an unexpected exception. When an exception is encountered, it is wrapped with an ExceptionHolder and pushed on the stack, providing easy access to the exception from within your result. Note: While you can configure exception mapping in your configuration file at any point, the configuration will not have any effect if this interceptor is not in the interceptor stack for your actions. It is recommended that you make this interceptor the first interceptor on the stack, ensuring that it has full access to catch any exception, even those caused by other interceptors.

Interceptor parameters:

  • logEnabled (optional) - Should exceptions also be logged? (boolean true|false)
  • logLevel (optional) - what log level should we use (trace, debug, info, warn, error, fatal)? - defaut is debug
  • logCategory (optional) - If provided we would use this category (eg. com.mycompany.app). Default is to usecom.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.

The parameters above enables us to log all thrown exceptions with stacktace in our own logfile, and present a friendly webpage (with no stacktrace) to the end user.

Extending the interceptor:

If you want to add custom handling for publishing the Exception, you may override publishException(com.opensymphony.xwork2.ActionInvocation, ExceptionHolder). The default implementation pushes the given ExceptionHolder on value stack. A custom implementation could add additional logging etc.

Example code:

 
 <xwork>
     <package name="default" extends="xwork-default">
         <global-results>
             <result name="success" type="freemarker">error.ftl</result>
         </global-results>

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

         <action name="test">
             <interceptor-ref name="exception"/>
             <interceptor-ref name="basicStack"/>
             <exception-mapping exception="com.acme.CustomException" result="custom_error"/>
             <result name="custom_error">custom_error.ftl</result>
             <result name="success" type="freemarker">test.ftl</result>
         </action>
     </package>
 </xwork>
 
 

This second example will also log the exceptions using our own category com.mycompany.app.unhandled at WARN level.

 
 <xwork>
   <package name="something" extends="xwork-default">
      <interceptors>
          <interceptor-stack name="exceptionmapping-stack">
              <interceptor-ref name="exception">
                  <param name="logEnabled">true</param>
                  <param name="logCategory">com.mycompany.app.unhandled</param>
                  <param name="logLevel">WARN</param>                               
              </interceptor-ref>  
              <interceptor-ref name="i18n"/>
              <interceptor-ref name="static-params"/>
              <interceptor-ref name="params"/>
              <interceptor-ref name="validation">
                  <param name="excludeMethods">input,back,cancel,browse</param>
              </interceptor-ref>
          </interceptor-stack>
      </interceptors>

      <default-interceptor-ref name="exceptionmapping-stack"/>
    
      <global-results>
           <result name="unhandledException">/unhandled-exception.jsp</result>
      </global-results>

      <global-exception-mappings>
           <exception-mapping exception="java.lang.Exception" result="unhandledException"/>
      </global-exception-mappings>
        
      <action name="exceptionDemo" class="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingAction">
          <exception-mapping exception="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingException"
                             result="damm"/>
          <result name="input">index.jsp</result>
          <result name="success">success.jsp</result>            
          <result name="damm">damm.jsp</result>
      </action>

   </package>
 </xwork>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值