
Exception 拦截器 对应的类为com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor
该拦截器是struts2专门用于处理异常的,如果我们进行了异常配置,当struts2的执行出现异常后可以跳转到指定的Result ,这也是struts2的异常处理机制。
下面看一下该拦截器的源码:
public class ExceptionMappingInterceptor extends AbstractInterceptor {//省略...@Overridepublic String intercept(ActionInvocation invocation) throws Exception {String result;try {result = invocation.invoke();//执行下一个拦截器} catch (Exception e) {if (isLogEnabled()) {handleLogging(e);}//获取异常映射配置List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();//从异常映射获取resultString mappedResult = this.findResultFromExceptions(exceptionMappings, e);if (mappedResult != null) {result = mappedResult;//如果获取的result不为null则替换原来的resultpublishException(invocation, new ExceptionHolder(e));//发布异常} else {throw e;}}return result;//返回result}//省略...}
限于篇幅,这里只列出了其最重要的intercept
方法,等用到其它方法的时候再列出。可以看到该拦截器继承自AbstractInterceptor
,AbstractInterceptor
实现了Interceptor
接口,AbstractInterceptor
很简单,就是对Intercepto
r接口中声明的init
与destroy
方法进行了空实现,这样做的好处理是如果我们自己写的拦截器继承自AbstractInterceptor
就不用去实现init
与destroy
方法了,如果需要这两个方法只要进行覆盖即可,这样比较方便,仅此而已。
该拦截器比较特殊,之所以说其特殊是因为其在defaultStack
中所处的位置还有其intercept
方法中invocation.invoke()
执行的地方。Exception拦截器处于defaultStack
中第一个位置,表面上是第一个位置,但大家可以看到result = invocation.invoke();
前面没有任何逻辑代码,而是直接执行下一个拦截器,加上越后执行的拦截器越先执行完成返回(如果该结论不清楚请参看struts2 18拦截器详解(一) ),这样就将导致result = invocation.invoke();
后面的代码是在其它所有拦截器执行完成后才会执行。struts2这样做的原因是由该拦截器的功能所决定的。因为该拦截器的目的是要对异常进行处理,而异常可以出现在每一个拦截器中,所以集中处理异常的代码要放到最后执行,所以这就决定了该拦截器要放置到defaultStack
的第一个位置,并且在intercept
方法中直接调用下一个拦截器。在struts2中可以定义多个拦截器栈,其实不管理是哪一个拦截器栈,Exception拦截器都应该是处理第一个位置。
result = invocation.invoke();
这里虽然只有一句,其实是包含了该拦截器后面的所有拦截器包括Action
的执行,如果在执行的过程中抛出异常则会进行到catch
块中,首先获取异常映射配置信息,然后从异常映射配置信息查找result
,如果找到了则将原来的result
进行替换,这样返回的result
就会指我们在配置文件中配置的异常Result
,以达到处理异常的目的。
下面是findResultFromExceptions
方法的源码:
protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t) {String result = null;// Check for specific exception mappings.if (exceptionMappings != null) {int deepest = Integer.MAX_VALUE;for (Object exceptionMapping : exceptionMappings) {ExceptionMappingConfig exceptionMappingConfig =(ExceptionMappingConfig) exceptionMapping;int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);if (depth >= 0 && depth < deepest) {deepest = depth;result = exceptionMappingConfig.getResult();}}}return result;}
getDepth
方法源码:
public int getDepth(String exceptionMapping, Throwable t) {return getDepth(exceptionMapping, t.getClass(), 0);}private int getDepth(String exceptionMapping, Class exceptionClass, int depth) {if (exceptionClass.getName().contains(exceptionMapping)) { //如果抛出的异常匹配到了则反加一个深度值// Found it!return depth;}// If we've gone as far as we can go and haven't found it...if (exceptionClass.equals(Throwable.class)) {//如果已经检索到了Throwable则返回-1return -1;}return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);}
findResultFromExceptions
方法中传入了异常映射配置信息,也就是在struts2配置文件中的<exception-mapping result="" exception=""/>
节点,因为我们可能配置多个<exception-mapping result="" exception=""/>
节点,所以传进来的是一个List
,如果不为null
则对其进行迭代。int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);
这句代码用于获取一个深度值,这句代码加上上下面的if语句判断决定了哪一种异常指向哪一个Result
。
首先声明了一个名为deepest
整形变量,初始值为Integer.MAX_VALUE
(这只是个初始值,该值会在后面迭代的过程被修改),在迭代的过程中,第一次代码depth
为0,如果在拦截器与Action
的执行过程中抛出的异常匹配到了我们配置的异常则会反这个深度值,如果没有匹配到则会继续匹配该抛出的异常类的父类,并且深度值加1,即这句代码:getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
这里使用了递归,其退出条件是匹配到了相应的异常,或者检索到了Throwable
类,因为所有异常都继承自Throwable
,如果检索到了Throwable
类就没必要再往上检索了,直接返回-1
(即没有匹配到配置的异常),获取到了depth
深度值后执行if
条件判断,这个判断很重要,我特意将它拿过来:
if (depth >= 0 && depth < deepest) {deepest = depth;result = exceptionMappingConfig.getResult();}
如果depth>=0&&depth<deepest
则会执行if
语句块中的代码,depth>=0
则表示这次迭代过程中匹配到了我们配置的异常,第一次迭代depth<deepest
是肯定成立的,所以第一次迭代如果匹配上了就会执行if
语句块中的代码,代码中将deepest
修改为了上一次返回的depth
值并对result
变量使用异常映射中的result
进行赋值。如果没有匹配到我们配置的异常则result
不会被赋值。在后面的迭代过程如果又匹配上了进行if判断的时候应该注意,这时的deepest
值是上一次匹配返回的深度值,如果这次返回的深度值比上一次返回的深度值小才会执行if语句块中的代码,深度值是通过递归调用得到了也就是depth
深度值越小则该异常越具体,因为匹配父异常的时候深度自加1
,所以当抛出的异常匹配上多个我们在配置文件中配置的异常映射的时候struts2会选择最具体的异常映射。举个例子,下面是异常配置:
<global-exception-mappings><exception-mapping result="error1" exception="java.lang.Exception"/><exception-mapping result="error2" exception="java.lang.IndexOutOfBoundsException"/></global-exception-mappings>
这里我们配置了两个异常映射,如果在拦截器与Action
的执行过程过程中抛出的是IndexOutOfBoundsException
则即会匹配第一个异常映射也会匹配第二个异常映射,但是第二个更具体即返回的深度值更小,所以在抛出IndexOutOfBoundsException
的时候struts2会选择error2
这个result
。
现在回到该拦截器的intercept
方法中。如果findResultFromExceptions
方法匹配到了我们配置的异常则会返回异常映射中的result
值,如果没有匹配上则返回null
,如果不为null的话,将原来的result
值替换为我们异常映射中的result
值并发布异常,如果为null
则返回原来的result
,在页面上就会显示出异常栈信息。这样如果匹配上了出来了异常到了前台显示的时候就会显示我们指定的页面。
下面看一下publishException(invocation, new ExceptionHolder(e));
方法所做的工作:
protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {invocation.getStack().push(exceptionHolder);}
该方法很简单,就是将抛出的异常包装进了一个叫ExceptionHolder
类型的对象中,然后压入了值栈的栈顶,这样我们就可以是视图中获取异常信息(如果需要的话)。
最后讲一下匹配异常的细节,就是:if (exceptionClass.getName().contains(exceptionMapping))
这个if
条件判断,这样struts2判断有无匹配上用的是contains
方法,即我们在配置异常映射的时候不需要将异常类名写全也可以,比如: <exception-mapping result="error2" exception="java.lang.IndexOutOfBoundsException"/>
我们配置成 <exception-mapping result="error2" exception="java.lang.IndexOutOf"/>
也可以匹配上IndexOutOfBoundsException
,这里个人觉得更好的做法应该是将所抛出的异常类的类名与配置异常映射判断是否相等,这样才更严谨,即contains
方法改为equals
方法。
--------------- END ---------------
每一篇文章都是小编精心为您准备的,写文不易,费时费脑;『分享』+『点赞』+『在看』三连击是小编坚持的最大动力,拜谢!




