Debug查看Struts2中ExceptionMappingInterceptor攔截器怎麼把ExceptionHolder放入值棧中,以及理解攔截器的工作原理。。。

來源:互聯網
上載者:User

標籤:

1、小案例代碼:

jsp介面:

 <body>    <s:property value="exceptionStack"/><br>    <s:property value="exception"/><br>    <s:property value="exception.getMessage"/>    <form action="product-details.action" method="post">    ProductName:<input type="text" name="productName"/><br>    ProductDesc:<input type="text" name="productDesc"/><br>    ProductPrice:<input type="text" name="productPrice"/><br>    <input type="submit" value="Submit"/>    </form>  </body>

 Action類(在其中故意寫了一個 java.lang.ArithmeticException: / by zero):

public String save() {        int i=1/0;        return "detailes";            }

struts.xml

<action name="product-details" class="com.luwenhu.struts2.Model.Product" method="save"><exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping><result name="input">/WEB-INF/pages/input.jsp</result><result name="detailes">/WEB-INF/pages/details.jsp</result></action>

 2、源碼分析

(1)從struts-default.xml中struts2提供了一個預設的攔截器棧(defaultStack)

(2)攔截器棧中有個exception攔截器,所屬類為com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor

(3)查看ExceptionMappingInterceptor這個類的intercept(攔截)方法,觀察其怎麼進行工作的。

觀察這個方法,攔截器在捕獲到異常e的時候會:

a.將這個異常交個handleLogging()方法

b.根據struts.xml的配置內容,通過ExceptionMappingConfig,配置異常結果處理

c.將異常處理放置(publish)到值棧中

3、debug驗證

(1)我們在捕獲到異常(191行)後進行斷點,然後觀察變數以及它的值,發現成功捕獲到java.lang.ArithmeticException: / by zero

                                  

(2)繼續執行到196行,mappingConfig對應著struts.xml中的異常配置

<-------->

                                                                                                                              

注意帶值棧棧頂此時還是test對象:

(3)  繼續執行代碼至201行,執行完之後,我門發現值棧的棧頂發生了變化。此時棧頂為ExceptionHolder,也就是說異常資訊成功放置到了值棧中

                                                                                                                                                                                        

就上就是ExceptionMappingInterceptor攔截器的工作原理!

 

Debug查看Struts2中ExceptionMappingInterceptor攔截器怎麼把ExceptionHolder放入值棧中,以及理解攔截器的工作原理。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.