exception-mapping element: Configuring declarative exception handling for the current action
There are 2 attributes in the exception-mapping element
Exception: Specifies the type of exception that needs to be captured. The full class name of the exception
Result: Specifies a response result that will be executed when the specified exception is caught, either from the declaration of the current action or from the Global-results declaration.
You can provide a global exception-capture mapping for your application through the global-exception-mappings element. However, any exception-mapping element declared under the Global-exception-mappings element can only refer to a result element declared under the Global-results element
The declarative exception-handling mechanism isThe Exceptionmappinginterceptor Interceptor is responsible for handling, when an exception declared by an exception-mapping element is captured, the Exceptionmappinginterceptor interceptor adds two objects to Valuestack:
Exception: Exception object that represents the exception being caught
Exceptionstack: The stack containing the caught exception
You can display the exception message on the view through the <s:property> tab I: Configure global exceptions (for specific actions) in the configuration file<Actionname=
"User_*"class=
"actions. Useraction "Method=
' {1} '> <exception-mappingresult=
"Input" Exception=
"Java.lang.Exception"> </exception-mapping> <resultname=
"Login_success"type=
"redirect">/success.jsp</result> <result name=
"Input">/index.jsp</result></Action>When the program has not been crawled exception, Exceptionmappinginterceptor first go to configure the file to see if there is any configuration of global exception information if any, the exception information will be placed in the stack, or will be reported 500 error: II: can be global-exception-mappings Element provides a global exception-capture mapping for an application<!--Global exception result type - <Global-results> <resultname=
"Input">/index.jsp</result> </Global-results> <!--Global exception capture -- <global-exception-mappings> <exception-mappingresult=
"Input"Exception=
"Java.lang.Exception"> </exception-mapping> </global-exception-mappings> Exceptionmappinginterceptor Interceptor Source Code Analysis:
Public
classExceptionmappinginterceptor
extends Abstractinterceptor { @Override
PublicString Intercept (actioninvocation invocation)
throws Exception {String result;
Try{result = Invocation.invoke (); }
Catch(Exception e) {
if(islogenabled ()) {handlelogging (e); } //Get exception configuration information in the configuration file list<exceptionmappingconfig> exceptionmappings = Invocation.getproxy (). GetConfig (). GetExceptionMappi NGS (); String Mappedresult =
This. Findresultfromexceptions (Exceptionmappings, e);
if(Mappedresult! =
NULL) {result = Mappedresult; //Press the exception information into the value stack publishexception (Invocation,
NewExceptionholder (e)); }
Else{
Throwe; } }
returnresult; } //press into the value stack
protected
voidpublishexception (actioninvocation invocation, Exceptionholder exceptionholder) {Invocation.getstack (). push (Exceptionholder); }}
Exception handling: exception-mapping elements