struts2:拦截器

下面我们再来分析另一个拦截器的实现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();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值