例外を一括処理する- ExceptionMappingInterceptor

ExceptionMappingInterceptorを利用すれば、アプリケーションで処理されない例外をエラーログに出力して専用のエラー画面に遷移させることができます。

ExceptionMappingInterceptorは、ValueStackに例外情報とスタックトレースをPushします。具体的にはPushされるのは、com.opensymphony.xwork2.interceptor.ExceptionHolderです。


struts.xmlの記述方法は以下のとおり。
各業務アプリのstrutsのパッケージ定義では、myapp-defaultを継承することが必要です。

<package name="myapp-default" extends="struts-default" abstract="true" >
  <interceptors>
    <!-- (1) -->
    <!-- 例外発生時にERRORレベルでログ出力してValueStackに例外情報をPush -->              
    <interceptor name="handle-exception"
      class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" >
      <param name="logEnabled">true</param>
      <param name="logLevel">error</param>
    </interceptor>

    <interceptor-stack name="myapp-defaultStack">
      <interceptor-ref name="handle-exception"/>
      ・・・
    </interceptor-stack>
  <interceptors>

  <default-interceptor-ref name="myapp-defaultStack" />

  <!-- (2) -->
  <!-- 例外が発生したらresultにsys_errorをセット -->
  <global-exception-mappings>
    <exception-mapping exception="java.lang.Throwable" result="sys_error" />
  </global-exception-mappings>

  <!-- (3) -->
  <!-- sys_errorが返されたら、error.jspに遷移 -->
  <global-results>
    <result name="sys_error" type="dispatcher" >
      <param name="location">/WEB-INF/view/eshop/common/error.jsp</param>
    </result>
  </global-results>
</package>


error.jspからは、以下のようにエラー情報を取得できます。

<H3>Error Message</H3>
<P><s:property value="exception.message" /></P>
<HR />

<H3>Technical Details</H3>
<P><s:property value="exceptionStack" /></P>



参考;