Struts2(七)---異常處理

· exception-mapping元素 : 配置當前action的聲明式異常處理
· exception-mapping元素中有兩個屬性
–exception : 指定需要捕獲的異常類型的全類名.
–result : 指定一個響應結果,該結果將在捕獲到指定異常時被執行,既可以來自當前action的聲明,也可以來自global-results聲明

<action name="input" >
          <exception-mapping result="/error.jsp" exception="java.lang.NullPointerException"></exception-mapping>
          <result>/success1.jsp</result>
       </action>
       <action name="input" >
          <result>/success.jsp</result>
       </action>

· 可以通過global-exception-mappings元素爲應用程序提供一個全局性的異常捕獲映射

    <package name="helloword" extends="struts-default" >
    //聲明瞭一個全局的異常響應結果,並定義了name屬性,在Package中所有的異常處理都可以引用該異常響應結果(即引用name屬性)
    <global-results>
       <result name="input1">/error.jsp</result>
    </global-results>
    <global-exception-mappings>
       <exception-mapping result="input1" exception="java.lang.Exception"></exception-mapping>
    </global-exception-mappings>
         <action name="input" >

          <result>/success1.jsp</result>
       </action>
       <action name="input" >
          <result>/success.jsp</result>
       </action>
       <action name="product-save" class="com.ty.exer.Product" method="save">

          <result name="details">/WEB-INF/pages/details.jsp</result>
       </action>

    </package>

· 聲明式異常處理機制由ExceptionMappingInterceptor攔截器負責處理,當某個exception-mapping元素聲明的異常被捕獲到時,ExceptionMappingInterceptor攔截器就會向ValueStack添加兩個屬性
~~exception : 表示被捕獲異常的Exception對象
~~exceptionStack : 包含着被捕獲異常的棧.

發佈了43 篇原創文章 · 獲贊 5 · 訪問量 4841
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章