Struts2运行流程图如下所示:
Struts2请求的处理步骤概述:
- 客户端发送请求;
- 请求经过一系列过滤器(Filter),起主要作用的是StrutsPrepareAndExecuteFilter过滤器(另有可选过滤器ActionContextCleanUp用于和其他框架集成);
- 调用FilterDispatcher,用于询问 ActionMapper当前该请求是否为Struts2请求(即是否返回一个非空的ActionMapping对象);
- 若ActionMapper认为该请求为Struts2请求,则FilterDispatcher将请求交给ActionProxy处理;
- ActionProxy通过Configuration Manager访问框架的配置文件,确定需要调用的Action 类及Action方法;
- ActionProxy创建一个ActionInvocation的实例,并对其进行初始化;
- ActionInvocation实例在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用;
- Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果;调用结果的execute()方法以渲染结果,渲染过程中可以使用Struts2框架所提供的标签;
- 执行各个拦截器invocation.invoke()之后的代码,并返回结果到客户端。
附:相关API
- ActionMapping:Simple class that holds the action mapping information used to invoke a Struts action. The name and namespace are required.
- ActionMapper:When given an HttpServletRequest, the ActionMapper may return null if no action invocation request matches, or it may return an ActionMapping that describes an action invocation for the framework to try.
- ActionProxy:ActionProxy is an extra layer between XWork and the action so that different proxies are possible.
- ActionInvocation:An ActionInvocation represents the execution state of an Action. It holds the Interceptors and the Action instance. By repeated re-entrant execution of the invoke() method, initially by the ActionProxy, then by the Interceptors, the Interceptors are all executed, and then the Action and the Result.