Debug See how the Struts2 exceptionmappinginterceptor interceptors put Exceptionholder into the value stack, and understand how interceptors work ...

Source: Internet
Author: User

1, small case code:

JSP interface:

<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>

The action class, in which a Java. Lang was deliberately written. 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. Source Code Analysis

(1) STRUTS2 provides a default interceptor stack (Defaultstack) from Struts-default.xml

(2) There is a exception interceptor in the interceptor stack, which belongs to the class Com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor

(3) Look at the Intercept (intercept) method of the Exceptionmappinginterceptor class to see how it works.

Observing this method, the interceptor catches the exception e when it:

A. Turn this exception into a handlelogging () method

B. Configure exception result handling based on Struts.xml configuration content, via Exceptionmappingconfig

C. Placing exception handling (publish) into the value stack

3. Debug verification

(1) We make a breakpoint after catching the exception (191 rows), then we observe the variable and its value, and discover that Java. Lang was successfully captured. ArithmeticException:/By zero

↓

(2) Continue to 196 rows, Mappingconfig corresponds to the exception configuration in the Struts.xml

<-------->

↓

Note the top of the stack stack at this point is still the test object:

(3) Continue executing the code to 201 lines, after the execution, we found that the stack top of the value stack has changed. At this point the stack top is exceptionholder, which means that the exception information is successfully placed in the value stack

                                                                                                                                                                                              &NBSP;&NBS P↓

That's how the Exceptionmappinginterceptor interceptor works!

Debug See how the Struts2 exceptionmappinginterceptor interceptors put Exceptionholder into the value stack, and understand how interceptors work ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.