拦截器:在某个事件发生之前进行拦截,做一些事前的处理,然后再执行被拦截的事件.
最典型的就是Servlet2.3规范中的Filter-过滤器,在Servlet执行之前,可以对输入参数进行处理,再将流程转交给对应的Servlet.
Xwork中的拦截器通过拦截Action,为其追加预处理和后处理.
Xwork 通过Interceptor 实现了这一步骤,从而我们可以根据需要,灵活的配置所需的
Interceptor。从而为Action提供可扩展的预处理、后处理过程。
Xwork 通过Interceptor 实现了这一步骤,从而我们可以根据需要,灵活的配置所需的
Interceptor。从而为Action提供可扩展的预处理、后处理过程。
ActionInvocation 是Xworks 中Action 调度的核心。而Interceptor 的调
度,也正是由ActionInvocation负责。
ActionInvocation 是一个接口, 而DefaultActionInvocation 则是Webwork 对
ActionInvocation的默认实现。
度,也正是由ActionInvocation负责。
ActionInvocation 是一个接口, 而DefaultActionInvocation 则是Webwork 对
ActionInvocation的默认实现。
Interceptor 的调度流程大致如下:
1.ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor
private void init() throws Exception {
……
List interceptorList = new
ArrayList(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
}
2.通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor:
public String invoke() throws Exception {
//调用intercaptors
if (interceptors.hasNext()) {
Interceptor interceptor =(Interceptor) interceptors.next();
resultCode = interceptor.intercept(this);
} else {
if (proxy.getConfig().getMethodName() == null) {
resultCode = getAction().execute();
} else {
resultCode = invokeAction(
getAction(),
proxy.getConfig()
);
}
}
……
}
1.ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor
private void init() throws Exception {
……
List interceptorList = new
ArrayList(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
}
2.通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor:
public String invoke() throws Exception {
//调用intercaptors
if (interceptors.hasNext()) {
Interceptor interceptor =(Interceptor) interceptors.next();
resultCode = interceptor.intercept(this);
} else {
if (proxy.getConfig().getMethodName() == null) {
resultCode = getAction().execute();
} else {
resultCode = invokeAction(
getAction(),
proxy.getConfig()
);
}
}
……
}
看看Xwork 是如何通过Interceptor,
将Webwork传入的Map类型数据结构,转换为Action所需的Java 模型对象。
数据转换的过程并不复杂:
⑴ 首先由ActionContext获得Map型的参数集parameters。
⑵ 由ActionContext获得值栈(OgnlValueStack)。
⑶ 遍历parameters中的各项数据。
⑷ 通过OgnlValueStack,根据数据的键值,为Model 对象填充属性数据。
OgnlValueStack 是 http://www.ognl.org/ 提供的一套可读写对象属性的类库(功能上有点类似
Jakarta Commons BeanUtils,以及Spring BeanWrapper)
将Webwork传入的Map类型数据结构,转换为Action所需的Java 模型对象。
数据转换的过程并不复杂:
⑴ 首先由ActionContext获得Map型的参数集parameters。
⑵ 由ActionContext获得值栈(OgnlValueStack)。
⑶ 遍历parameters中的各项数据。
⑷ 通过OgnlValueStack,根据数据的键值,为Model 对象填充属性数据。
OgnlValueStack 是 http://www.ognl.org/ 提供的一套可读写对象属性的类库(功能上有点类似
Jakarta Commons BeanUtils,以及Spring BeanWrapper)
OgnlValueStack使用非常简单,下面这段示例代码将User 对象的name 属性设为“erica”。
OgnlValueStack stack = new OgnlValueStack();
stack.push(new User());//首先将欲赋值对象压入栈中
stack.setValue("name","erica");//为栈顶对象的指定属性名赋值
OgnlValueStack stack = new OgnlValueStack();
stack.push(new User());//首先将欲赋值对象压入栈中
stack.setValue("name","erica");//为栈顶对象的指定属性名赋值
DefaultActionInvocation.init方法中有入栈的动作