struts2 -----interceptor 之 ActionInvocation 意义
来源:互联网 发布:centos snmp 版本 编辑:程序博客网 时间:2024/05/17 04:23
“将Web页面中的输入元素封装为一个(请求)数据对象”,这个对象就是ActionInvocation类型.
对于Xwork 而言,前端的Webwork 组件为其提供的是一个Map 类型的数据结构。而Action面向的却是Model对象所提供的数据结构。在何时、何处对这两种不同的数据结构进行转换?
写一个辅助类完成这样的工作,并在每次Action 调用之前由框架代码调用他完成转换工作。
Xwork 通过Interceptor 实现了这一步骤,从而我们可以根据需要,灵活的配置所需的Interceptor。从而为Action提供可扩展的预处理、后处理过程。
ActionInvocation 是Xworks 中Action 调度的核心。而对Interceptor 的调度,也正是由ActionInvocation负责。
ActionInvocation 是一个接口, 而DefaultActionInvocation 则是Webwork 对ActionInvocation的默认实现。
Interceptor 的调度流程大致如下:
1. ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor。
参见ActionInvocation.init方法中相关代码:
对于Xwork 而言,前端的Webwork 组件为其提供的是一个Map 类型的数据结构。而Action面向的却是Model对象所提供的数据结构。在何时、何处对这两种不同的数据结构进行转换?
写一个辅助类完成这样的工作,并在每次Action 调用之前由框架代码调用他完成转换工作。
Xwork 通过Interceptor 实现了这一步骤,从而我们可以根据需要,灵活的配置所需的Interceptor。从而为Action提供可扩展的预处理、后处理过程。
ActionInvocation 是Xworks 中Action 调度的核心。而对Interceptor 的调度,也正是由ActionInvocation负责。
ActionInvocation 是一个接口, 而DefaultActionInvocation 则是Webwork 对ActionInvocation的默认实现。
Interceptor 的调度流程大致如下:
1. ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor。
参见ActionInvocation.init方法中相关代码:
Java代码:
private void init()throws Exception ...{
……
List interceptorList = new
ArrayList(proxy.getConfig().getInterceptors());
interceptors
……
List interceptorList = new
ArrayList(proxy.getConfig().getInterceptors());
interceptors
2. 通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor:
下面是DefaultActionInvocation中Action调度代码:
Java代码:
下面是DefaultActionInvocation中Action调度代码:
Java代码:
public String <SPAN class=undefined>invoke</SPAN>() throws Exception ...{
if (executed)
throw new IllegalStateException("Action has already executed");
if (interceptors.hasNext()) ...{
Interceptor interceptor = (Interceptor) interceptors.next();
resultCode = interceptor.intercept(this);
} else
resultCode = <SPAN class=undefined>invoke</SPAN>Action(getAction(), proxy.getConfig());
if (!executed) ...{
if (preResultListeners != null) ...{
Iterator iterator = preResultListeners.iterator();
while (iterator.hasNext()) ...{
PreResultListener listener
= (PreResultListener) iterator.next();
listener.beforeResult(this, resultCode);
}
}
if (proxy.getExecuteResult())
executeResult();
executed = true;
}
return resultCode;
}
if (executed)
throw new IllegalStateException("Action has already executed");
if (interceptors.hasNext()) ...{
Interceptor interceptor = (Interceptor) interceptors.next();
resultCode = interceptor.intercept(this);
} else
resultCode = <SPAN class=undefined>invoke</SPAN>Action(getAction(), proxy.getConfig());
if (!executed) ...{
if (preResultListeners != null) ...{
Iterator iterator = preResultListeners.iterator();
while (iterator.hasNext()) ...{
PreResultListener listener
= (PreResultListener) iterator.next();
listener.beforeResult(this, resultCode);
}
}
if (proxy.getExecuteResult())
executeResult();
executed = true;
}
return resultCode;
}
所有的拦截器都必须实现Interceptor 接口。
public interface Interceptor {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
在Interceptor 实现中,抽象实现AroundInterceptor得到了最广泛的应用(扩展),它增加了预处理(before)和后处理(after)方法的定义。
AroundInterceptor.java:
public interface Interceptor {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
在Interceptor 实现中,抽象实现AroundInterceptor得到了最广泛的应用(扩展),它增加了预处理(before)和后处理(after)方法的定义。
AroundInterceptor.java:
Java代码:
public abstractclass AroundInterceptor implements Interceptor
...{
protected Log log = LogFactory.getLog(this.getClass());
public void destroy() ...{
}
public void init() ...{
}
public String intercept(<SPAN class=hilite1>ActionInvocation</SPAN> invocation) throws Exception ...{
String result = null;
before(invocation);
result = invocation.<SPAN class=undefined>invoke</SPAN>();
after(invocation, result);
return result;
}
protected abstractvoid after
(<SPAN class=hilite1>ActionInvocation</SPAN> <SPANclass=hilite1>actioninvocation</SPAN>, String string)throws Exception;
protected abstractvoid before(<SPAN class=hilite1>ActionInvocation</SPAN> <SPAN class=hilite1>actioninvocation</SPAN>)
throws Exception;
}
...{
protected Log log = LogFactory.getLog(this.getClass());
public void destroy() ...{
}
public void init() ...{
}
public String intercept(<SPAN class=hilite1>ActionInvocation</SPAN> invocation) throws Exception ...{
String result = null;
before(invocation);
result = invocation.<SPAN class=undefined>invoke</SPAN>();
after(invocation, result);
return result;
}
protected abstractvoid after
(<SPAN class=hilite1>ActionInvocation</SPAN> <SPANclass=hilite1>actioninvocation</SPAN>, String string)throws Exception;
protected abstractvoid before(<SPAN class=hilite1>ActionInvocation</SPAN> <SPAN class=hilite1>actioninvocation</SPAN>)
throws Exception;
}
AroundInterceptor.invoke 方法中,调用了参数invocation的invoke 方法。
最后,结合最常用的ParametersInterceptor,看看Xwork 是如何通过Interceptor,将Webwork传入的Map类型数据结构,转换为Action所需的Java 模型对象。
ParametersInterceptor.java:
Java代码:
最后,结合最常用的ParametersInterceptor,看看Xwork 是如何通过Interceptor,将Webwork传入的Map类型数据结构,转换为Action所需的Java 模型对象。
ParametersInterceptor.java:
Java代码:
public class ParametersInterceptorextends AroundInterceptor ...{
protected void after(<SPANclass=hilite1>ActionInvocation</SPAN> dispatcher, String result)
throws Exception ...{
}
protected void before(<SPANclass=hilite1>ActionInvocation</SPAN> invocation)throws Exception
...{
if (!(invocation.getAction() instanceof NoParameters)) ...{
final Map parameters =
ActionContext.getContext().getParameters(); ⑴
if (log.isDebugEnabled()) ...{
log.debug("Setting params " + parameters);
}
ActionContext invocationContext =
invocation.getInvocationContext();
try ...{
invocationContext.put(
InstantiatingNullHandler.CREATE_NULL_OBJECTS,
Boolean.TRUE);
invocationContext.put(
XWorkMethodAccessor.DENY_METHOD_EXECUTION,
Boolean.TRUE);
invocationContext.put(
XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.TRUE);
if (parameters != null) ...{
final OgnlValueStack stack =
ActionContext.getContext().getValueStack(); ⑵
for (Iterator iterator =parameters.entrySet().iterator();
iterator.hasNext();
) ...{
Map.Entry entry = (Map.Entry) iterator.next();
stack.setValue( ⑷
entry.getKey().toString(),
entry.getValue());
}
}
} finally ...{
invocationContext.put(
InstantiatingNullHandler.CREATE_NULL_OBJECTS,
Boolean.FALSE);
invocationContext.put(
XWorkMethodAccessor.DENY_METHOD_EXECUTION,
Boolean.FALSE);
invocationContext.put(
XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.FALSE);
}
}
}
}
protected void after(<SPANclass=hilite1>ActionInvocation</SPAN> dispatcher, String result)
throws Exception ...{
}
protected void before(<SPANclass=hilite1>ActionInvocation</SPAN> invocation)throws Exception
...{
if (!(invocation.getAction() instanceof NoParameters)) ...{
final Map parameters =
ActionContext.getContext().getParameters(); ⑴
if (log.isDebugEnabled()) ...{
log.debug("Setting params " + parameters);
}
ActionContext invocationContext =
invocation.getInvocationContext();
try ...{
invocationContext.put(
InstantiatingNullHandler.CREATE_NULL_OBJECTS,
Boolean.TRUE);
invocationContext.put(
XWorkMethodAccessor.DENY_METHOD_EXECUTION,
Boolean.TRUE);
invocationContext.put(
XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.TRUE);
if (parameters != null) ...{
final OgnlValueStack stack =
ActionContext.getContext().getValueStack(); ⑵
for (Iterator iterator =parameters.entrySet().iterator();
iterator.hasNext();
) ...{
Map.Entry entry = (Map.Entry) iterator.next();
stack.setValue( ⑷
entry.getKey().toString(),
entry.getValue());
}
}
} finally ...{
invocationContext.put(
InstantiatingNullHandler.CREATE_NULL_OBJECTS,
Boolean.FALSE);
invocationContext.put(
XWorkMethodAccessor.DENY_METHOD_EXECUTION,
Boolean.FALSE);
invocationContext.put(
XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.FALSE);
}
}
}
}
数据转换的过程并不复杂:
⑴ 首先由ActionContext获得Map型的参数集parameters。
⑵ 由ActionContext获得值栈(OgnlValueStack)。
⑶ 遍历parameters中的各项数据。
⑷ 通过OgnlValueStack,根据数据的键值,为Model 对象填充属性数据。
OgnlValueStack 是[url]http://www.ognl.org4[/url]提供的一套可读写对象属性的类库
上面的代码中并没有发现将Model对象入栈的部分,是由于ActionInvocation在初始化的时候已经预先完成了压栈工作,如DefaultActionInvocation.init方法中代码所示:
private void init() throws Exception {
Map contextMap = createContextMap();
createAction();
if (pushAction) {
stack.push(action); //压栈
}
……
}
- struts2 -----interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation 意义
- webwork拦截器interceptor 之 ActionInvocation 意义
- struts2拦截器interceptor 之 ActionInvocation 意
- webwork拦截器interceptor 之 ActionInvocation 意义(转载)
- webwork拦截器interceptor 之 ActionInvocation 意义(转载)
- webwork拦截器interceptor 之 ActionInvocation
- Struts2之ServletActionContext、ActionContext、ActionInvocation
- Struts2-Interceptor的逻辑意义
- ActionContext 、Interceptor和ActionInvocation
- Struts2 ActionInvocation
- struts2 之 Interceptor
- 5. Struts2框架之 Interceptor
- Struts2中ActionInvocation
- 关于在struts2中interceptor 中使用ActionInvocation 对象获得HttpServletRequest中封装的参数,以Map的形式
- Struts2源代码分析之Struts2 Interceptor模式
- Struts2之Struts2-2.5.5 Interceptor
- 如何绕过防注入
- grails-数据库编程(高级GORM特性)
- IIS oledb jet 4.0 读取excel 时,出现未指定错误 解决方法 !
- sql 的触发器
- sql 五种约束 规则 默认值
- struts2 -----interceptor 之 ActionInvocation 意义
- ScriptManager.RegisterStartupScript方法
- 一位软件工程师的6年总结
- oracle存储过程实现增删改简单示例
- 调用存储过程
- VisualBasic6 视频教程
- grails-数据库编程(详见http://www.kuqin.com/grails-doc-1.0/guide/single.html)
- Hibernate学习笔记14 核心开发接口(重点)
- 天气预报 获取节气的代码