WebWork2源码分析续二

王朝java/jsp·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

下面我们再来分析另一个拦截器的实现ModelDrivenInterceptor,首先说说他的设计目的,我们知道在Struts中通常有一个ActionFormBean他是用来封装请求数据的,在WebWork2.x中这一功能得到了进一步的发挥,他可以实现两中Action驱动模式,他们都是信息携带者.

Property-Driven

Model-Driven

最通俗的解释就是, Property-Driven通过属性来贯穿整个MVC,而Model-Driven就是通过Model对象来贯穿整个MVC.

他们的存在方式: Model-Drive就是独立的一个类,而Property-Driven则必须依附于你自定义的Action类

如果我们用Model-Drive方式,那么就必须在配置文件中添加ModelDrivenInterceptor拦截器,由这个拦截器向我们的Model Bean中传递值,且你的Action中 也必须实现ModelDriven接口,用于获取该Model Bean,下面来看看这个拦截器的具体实现,来做进一步的分析,代码如下:

public class ModelDrivenInterceptor extends AroundInterceptor {

//~ Methods ////////////////////////////////////////////////////////////////

protected void after(ActionInvocation dispatcher, String result) throws Exception { }

protected void before(ActionInvocation invocation) throws Exception {

Action action = invocation.getAction();

if (action instanceof ModelDriven) {

//判断该Action是否实现了ModelDriven接口,如果实现了这个ModelDriven接口,他将向这个Action所对应的Model传递信息

ModelDriven modelDriven = (ModelDriven) action;

OgnlValueStack stack = invocation.getStack();

// 用于获取Action中的Model Bean,并压入OgnlValueStack

stack.push(modelDriven.getModel());

}

}

}

关于OgnlValueStack 的具体信息请参考 http://www.ognl.org

从上面的这个public String intercept(ActionInvocation invocation) 方法中我们可以看出所有的拦截器都是通过ActionInvocation来执行调度的,我们可以称DefaultActionInvocation为XWork1.x的调度器,说的这里我想各位对WebWork2.x的拦截器也有了一个大概的了解.

既然DefaultActionInvocation是xWork1.x的调度器,不分析他是说不过去的,接下来我们分析ActionInvocation的实现者DefaultActionInvocation的源码,已窥其究竟

由于之前也分析过DefaultActionInvocation的一些代码, 下面则节选部分还没有分析的代码来完成简要的分析

public class DefaultActionInvocation implements ActionInvocation {

//在这里Result是一个接口,而ActionChainResult是他的一个实现,他的目的是用于处理Action Chain的,在这里对Action Chain做以下说明:

通常一个Action执行完毕,要么是返回表单,要么返回另外一个Action,来继续执行, 如果返回了Action则就形成了Action Chain 动作链,然后继续执行这个新的Action,直到返回一个non-chain结果

public Result getResult() throws Exception {

Result returnResult = result;

// If we've chained to other Actions, we need to find the last result

while (returnResult instanceof ActionChainResult) {

ActionProxy aProxy = ((ActionChainResult) returnResult).getProxy();

if (aProxy != null) {

Result proxyResult = aProxy.getInvocation().getResult();

if ((proxyResult != null) && (aProxy.getExecuteResult())) {

returnResult = proxyResult;

} else { break; }

} else { break; }

}

return returnResult;

}

//返回Stack

public OgnlValueStack getStack() {

return stack;

}

//下面的方法我就不在赘述了,我想大家就是看名称也应该可以了解一二,如果有什么疑问,请参考前面的分析

protected void createAction() {

// load action

try {

action = ObjectFactory.getObjectFactory().buildAction(proxy.getConfig());

}

protected String invokeAction(Action action, ActionConfig actionConfig) throws Exception {

if (proxy.getConfig().getMethodName() == null) {

return getAction().execute();

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航