代码清单17:调用截拦器,DefaultActionInvocation.invoke()方法的部分代码
if (interceptors.hasNext()) {
//从截拦器集合中取出当前的截拦器
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
UtilTimerStack.profile("interceptor: "+interceptor.getName(),
new UtilTimerStack.ProfilingBlock<String>() {
public String doProfiling() throws Exception {
//执行截拦器(Interceptor)接口中定义的intercept方法
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
returnnull;
}
});
}
从代码中似乎看不到截拦器的递归调用,其实是否递归完全取决于程序员对程序的控制,先来看一下Interceptor接口的定义:
代码清单18:Interceptor.java
publicinterface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
所有的截拦器必须实现intercept方法,而该方法的参数恰恰又是ActionInvocation,所以,如果在intercept方法中调用invocation.invoke(),代码清单17会再次执行,从Action的Intercepor列表中找到下一个截拦器,依此递归。下面是一个自定义截拦器示例: