您的位置:首页 > 编程语言 > Java开发

Struts2请求过程原理分析

2015-05-02 20:00 232 查看
  1.org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter()方法中

public String invoke() throws Exception {
String profileKey = "invoke: ";
try {
UtilTimerStack.push(profileKey);

if (executed) {
throw new IllegalStateException("Action has already executed");
}

if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {            //首先会顺序的递归执行当前Action中所配置的所有的拦截器, 直到拦截器遍历完毕调用真正的Action
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
} else {         //通过调用DefaultActionInvocation的invokeActionOnly()方法去反射调用Action, 调用过程中, 会从当前的invocation对象中得到Action的实例,          // 并通过代理对象获得配置信息.
resultCode = invokeActionOnly();
}


DefaultActionInvocation.invoke()

首先会顺序的递归执行当前Action中所配置的所有的拦截器, 直到拦截器遍历完毕调用真正的Action

通过调用DefaultActionInvocation的invokeActionOnly()方法去反射调用Action, 过程中, 会从当前的invocation对象中得到Action的实例, 并通过代理对象获得配置信息.

  5.com.opensymphony.xwork2.DefaultActionInvocation.invokeAction()方法:

反射获得当前请求分发的Action中的方法对象

以传入的Action实例反射调用该处理方法

如果返回的结果不是com.opensymphony.xwork2.Result的实例, 返回强转为字符串的结果.

  6.回到1.4所在方法继续:

将invokeAction返回的resultCode存储到当前的invokeAction实例中

进入result处理

  7.com.opensymphony.xwork2.DefaultActionInvocation.executeResult()方法:

创建result对象(com.opensymphony.xwork2.DefaultActionInvocation.createResult()方法)

如果当时调用Action返回了Result对象, 则直接返回

否则, 通过proxy对象获取配置信息, 根据resultCode获取到Result对象

使用ognl翻译result中的用${}和%{}标识引用ValueStack中的值的参数.

调用执行result

  8.org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute()方法

准备执行环境: request, pageContext等等

判断有没有合适的分发器, 如果没有, 发404消息

在request对象中设定两个属性:

struts.view_uri: 分发之后的url地址

struts.request_uri: 真正请求的url地址

发送真正的响应信息

  9.至此, Action的处理完毕, 回归到1.4.1中指明的递归调用那里, 逐步的返回并执行每个拦截器的invokeAction.invoke()后面的代码, 从这里也就可以看出拦截器的机制.

c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: