歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2 聲明式異常處理

Struts2 聲明式異常處理

日期:2017/3/1 9:14:22   编辑:Linux編程

1.通過配置的方式捕獲指定類型異常,由 ExceptionMappingInterceptor 攔截器將異常信息(ExceptionHolder:exceptionStack,exception)壓入棧頂,然後通過 OGNL 表達式在頁面中獲取異常信息。

2.Action 級別,只對當前 Action 對應類型的異常起作用,在 struts2 配置文件 action 節點內部使用 exception-mapping 來映射異常。如:

<action name="arithmeticAction" class="com.nucsoft.struts2.helloworld.ExceptionAction" method="arithmeticAction">
<exception-mapping exception="java.lang.ArithmeticException" result="arithmeticException"/>
<result>/success.jsp</result>
<result name="arithmeticException">/arithmeticException.jsp</result>
</action>

可以在錯誤頁面獲取異常信息:<s:property value="exception"/>

3.GLOBAL 級別,針對所有 Action 對應類型的異常都起作用

<global-results>
<result name="globalException">/exception.jsp</result>
</global-results>

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

頁面中,可以通過 OGNL 表達式獲取值棧中的異常信息。

注意:

(1)GLOBAL 級別的聲明式異常配置必須配置在所有 action 之前,同時,global-results 節點需要配置在 global-exception-mappings 之前。

(2)當捕獲到一個異常對象時,如果同時存在Action和全局兩種級別的映射信息,則優先使用Action級別

Copyright © Linux教程網 All Rights Reserved