Struts2(十二)---运行流程

(一)运行流程图
Struts2(十二)---运行流程
(二)运行流程详解
① client初始化一个指向Servlet容器(比如Tomcat)的请求;

② 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其它框架的集成非常有帮助,比如:SiteMesh Plugin);

注:从struts2.1.3后就不须要配置ActionContextCleanUp过滤器了,该过滤器的作用是清空值栈等,但struts会留下自己session和Attribute等

③ 接着调用struts2的StrutsPrepareAndExecuteFilter过滤器,StrutsPrepareAndExecuteFilter询问ActionMapper:该请求是否为一个Struts2请求(即是否返回一个非空的ActionMapping对象);

备注:早期struts用FilterDispatcher过滤器,如今普遍都用StrutsPrepareAndExecuteFilter。 就名字而言,prepare与execute前者表示准备说指filterinit方法即配置导入;后者表示进行过滤指doFilter方法即request请求转发给相应action去处理
④若ActionMapper认为该请求是一个Struts2请求,则StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy;
⑤ActionProxy通过Configuration Manager询问框架的配置文件,确定需要调用的Action类及Action方法;
⑥ActionProxy创建一个ActionInvocation的实例,并进行初始化;
⑦ActionInvocation实例在调用Action的过程前后,涉及到相关拦截器(Interceptor)的调用;
⑧Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果.调用的结果的execute方法渲染结果,在渲染的过程中可以使用struts2框架中的标签;
⑨执行各个拦截器invocation.invoke()之后的方法;
(三)Struts2执行过程相关的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.