Action的执行机制

[color=green]ActionProxyFactory: [/color]
create ActionProxy factory,create ActionInvocation,create ActionProxy
[color=green]ActionProxy: [/color]
serves as client code's handle to execute an action,it holds an ActionInvocation which reprents the current satate of the execution of the action.ActionProxy is created by a ActionProxyFactory in a dispatcher.ActionProxy sets up the execute context for the ActionInvocation and then calls invoke() on the ActionInvocation.
[color=green]ActionInvocation: [/color]reprents the current state of the execution of the action,it holds the action instance ,the interceptors to be applied,the map of results,and an ActionContext.
[color=green]ActionContext:[/color] which includes the request parameters,the application map ,the session map,the Local, and the ServletRequest and ServletResponse.

执行过程如下:
(1)在web.xml中指定的ServletDispacter或者FilterDispatcher,负责调用ActionProxyFactory完成ActionProxy的创建,
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, context);
(2)ActionProxy 为ActionInvocation设定执行环境上下文,并调用ActionInvocation的invoke()方法
(3)关键在于ActionInvocation的invoke()方法,


public String invoke() throws Exception {
if (executed) {
throw new IllegalStateException("Action has already executed");
}
if (interceptors.hasNext()) { // notice here is if ,not while
InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
/** 调用拦截器的方法,将ActionInvocation作为参数
* 在intercept(this);中回调invoke() 方法,是一种递归的方式
*/
resultCode = interceptor.getInterceptor().intercept(this);
} else {
resultCode = invokeActionOnly();//当没有了拦截器没有调用时就invokeAction!然后返回!因为是递归调用的!然后按反顺序返回进行剩下的没完成部分...
}

// 做结果返回前的处理
// this is needed because the result will be executed, then control will return to the Interceptor, which will
// return above and flow through again
if (!executed) {
if (preResultListeners != null) {
for (Iterator iterator = preResultListeners.iterator();
iterator.hasNext();) {
PreResultListener listener = (PreResultListener) iterator.next();
listener.beforeResult(this, resultCode);
}
}
// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
//执行结果的返回
executeResult();
}
executed = true;
}
return resultCode;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值