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