java 拦截器ajax_(转)拦截器深入实践 - JAVA & XML & JAVASCRIPT & AJAX & CSS - BlogJava

Interceptor的定义

我们来看一下Interceptor的接口的定义:

Java代码 icon_copy.gif

publicinterfaceInterceptorextendsSerializable {

/**

* Called to let an interceptor clean up any resources it has allocated.

*/

voiddestroy();

/**

* Called after an interceptor is created, but before any requests are processed using

* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving

* the Interceptor a chance to initialize any needed resources.

*/

voidinit();

/**

* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the

* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.

*

* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.

* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.

*/

String intercept(ActionInvocation invocation)throwsException;

}

public interface Interceptor extends Serializable {

/**

* Called to let an interceptor clean up any resources it has allocated.

*/

void destroy();

/**

* Called after an interceptor is created, but before any requests are processed using

* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving

* the Interceptor a chance to initialize any needed resources.

*/

void init();

/**

* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the

* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.

*

* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.

* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.

*/

String intercept(ActionInvocation invocation) throws Exception;

}

Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者。

我们再来看看一个典型的Interceptor的抽象实现类:

Java代码 icon_copy.gif

publicabstractclassAroundInterceptorextendsAbstractInterceptor {

/* (non-Javadoc)

* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)

*/

@Override

publicString intercept(ActionInvocation invocation)throwsException {

String result =null;

before(invocation);

// 调用下一个拦截器,如果拦截器不存在,则执行Action

result = invocation.invoke();

after(invocation, result);

returnresult;

}

publicabstractvoidbefore(ActionInvocation invocation)throwsException;

publicabstractvoidafter(ActionInvocation invocation, String resultCode)throwsException;

}

public abstract class AroundInterceptor extends AbstractInterceptor {

/* (non-Javadoc)

* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)

*/

@Override

public String intercept(ActionInvocation invocation) throws Exception {

String result = null;

before(invocation);

// 调用下一个拦截器,如果拦截器不存在,则执行Action

result = invocation.invoke();

after(invocation, result);

return result;

}

public abstract void before(ActionInvocation invocation) throws Exception;

public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;

}

在这个实现类中,实际上已经实现了最简单的拦截器的雏形。或许大家对这样的代码还比较陌生,这没有关系。我在这里需要指出的是一个很重要的方法invocation.invoke()。这是ActionInvocation中的方法,而ActionInvocation是Action调度者,所以这个方法具备以下2层含义:

1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。

2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。

所以,我们可以发现,invocation.invoke()这个方法其实是整个拦截器框架的实现核心。基于这样的实现机制,我们还可以得到下面2个非常重要的推论:

1. 如果在拦截器中,我们不使用invocation.invoke()来完成堆栈中下一个元素的调用,而是直接返回一个字符串作为执行结果,那么整个执行将被中止。

2. 我们可以以invocation.invoke()为界,将拦截器中的代码分成2个部分,在invocation.invoke()之前的代码,将会在Action之前被依次执行,而在invocation.invoke()之后的代码,将会在Action之后被逆序执行。

由此,我们就可以通过invocation.invoke()作为Action代码真正的拦截点,从而实现AOP。

Interceptor拦截类型

从上面的分析,我们知道,整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上,我们也正式根据这句代码的调用位置,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类:

1. before

before拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,将依照拦截器定义的顺序,顺序执行。

2. after

after拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,将一招拦截器定义的顺序,逆序执行。

3. PreResultListener

有的时候,before拦截和after拦截对我们来说是不够的,因为我们需要在Action执行完之后,但是还没有回到视图层之前,做一些事情。Struts2同样支持这样的拦截,这种拦截方式,是通过在拦截器中注册一个PreResultListener的接口来实现的。

Java代码 icon_copy.gif

publicinterfacePreResultListener {

/**

* This callback method will be called after the Action execution and before the Result execution.

*

* @param invocation

* @param resultCode

*/

voidbeforeResult(ActionInvocation invocation, String resultCode);

}

public interface PreResultListener {

/**

* This callback method will be called after the Action execution and before the Result execution.

*

* @param invocation

* @param resultCode

*/

void beforeResult(ActionInvocation invocation, String resultCode);

}

在这里,我们看到,Struts2能够支持如此多的拦截类型,与其本身的数据结构和整体设计有很大的关系。正如我在之前的文章中所提到的:

downpour 写道

因为Action是一个普通的Java类,而不是一个Servlet类,完全脱离于Web容器,所以我们就能够更加方便地对Control层进行合理的层次设计,从而抽象出许多公共的逻辑,并将这些逻辑脱离出Action对象本身。

我们可以看到,Struts2对于整个执行的划分,从Interceptor到Action一直到Result,每一层都职责明确。不仅如此,Struts2还为每一个层次之前都设立了恰如其分的插入点。使得整个Action层的扩展性得到了史无前例的提升。

Interceptor执行顺序

Interceptor的执行顺序或许是我们在整个过程中最最关心的部分。根据上面所提到的概念,我们实际上已经能够大致明白了Interceptor的执行机理。我们来看看Struts2的Reference对Interceptor执行顺序的一个形象的例子。

如果我们有一个interceptor-stack的定义如下:

Xml代码 icon_copy.gif

那么,整个执行的顺序大概像这样:

23045c94-b72a-3c04-9c6c-06ad4392d743.gif

在这里,我稍微改了一下Struts2的Reference中的执行顺序示例,使得整个执行顺序更加能够被理解。我们可以看到,递归调用保证了各种各样的拦截类型的执行能够井井有条。

请注意在这里,每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值