Detailed explanation: Struts2 working principle and execution flow chart [transfer]

[Note: Original link https://blog.csdn.net/wjw0130/article/details/46371847]
Struts2 is a set of very good web application framework, which is elegant, powerful and simple to use. It can be said that Struts2 is a very mature MVC architecture.

When we learn Struts2, it is best to learn its operation process and core concepts first, get inspiration from it, and improve ourselves, not just how to use it.

Saw this sentence online:

You must not become a programmer who can only use the framework proficiently. In that case, you will be exhausted. You may only use Hadoop forever, and you will not be able to write a Hadoop. You are just a Hadoop programmer, not a distributed engineer.
You may only ever use Struts and forget to write your own filter, you are just an SSH programmer, not a Web engineer.

Without further ado, let's walk into Struts2 together

1. System Architecture

The official documentation of Struts2 comes with the architecture diagram of Struts2. From this picture, Struts2 can be well understood


About the Key in the figure:

  • Servlet Filters: Filter chain, all requests from the client must be processed by the Filter chain.
  • Struts Core: The core part of Struts2, but Struts2 has done it for us, we don't need to do this
  • Interceptors, the interceptor of Struts2. Struts2 provides many default interceptors, which can complete most of the daily development work; and our custom interceptors are used to implement the functions required by the actual customer business.
  • User Created, created by developers, including struts.xml, Action, Template, these are what everyone who uses Struts2 for development must know.


  • 1. FilterDispatcher is the dispatch center of the entire Struts2, that is, the C (control center) in MVC. It decides whether to process the request according to the result of the ActionMapper. If the ActionMapper points out that the URL should be processed by Struts2, then it will perform the Action processing, and Stops filters in the filter chain that have not yet executed.
  • 2. ActionMapper will determine whether the request should be processed by Struts2. If Struts2 processing is required, ActionMapper will return an object to describe the information of the ActionInvocation corresponding to the request.
  • 3.ActionProxy , it will create an ActionInvocation instance, which is located between Action and xwork, so that we have the opportunity to introduce more implementation methods in the future, such as through WebService.
  • 4.ConfigurationManager is the management center of xwork configuration, which can be regarded as the corresponding configuration file of struts.xml in memory.
  • 5.struts.xml, is where developers must visit. It is the application configuration file of Stuts2, which is responsible for the configuration of the mapping relationship between URL and Action, and the Result configuration of page jump after execution.
  • 6.ActionInvocation : It actually calls and executes the Action. It has an Action instance and the interceptor instance that the Action depends on. ActionInvocation will execute these interceptors, Actions and corresponding Result in the specified order.
  • Interceptor: It is the cornerstone of Struts2, similar to JavaWeb's Filter. Interceptors are some stateless classes. Interceptors can automatically intercept Actions. They provide developers with the ability to execute some actions before Action runs or after Result runs. opportunity for function code.
  • 7.Action:用来处理请求,封装数据。

  • 二、运行流程

1.当用户的发出请求,比如http:localhost:8080/Struts2/helloworld/helloworldAction.action,请求会被Tomcat接收到,Tomcat服务器来选择处理这个请求的Web应用,那就是由helloworld这个web工程来处理这个请求。
2.Web容器会去读取helloworld这个工程的web.xml,在web.xml中进行匹配,但发现,由struts2这个过滤器来进行处理(也就是
StrutsPrepareAndExecuteFilter),根据Filter的配置,找到FilterDispatcher(Struts2的调度中心
3.然后会获取FilterDispatcher实例,然后回调doFilter方法,进行真正的处理
PS:FilterDispatcher是任何一个Struts2应用都需要配置的,通常情况下,web.xml文件中还有其他过滤器时,FilterDispatcher是放在滤器链的最后;如果在FilterDispatcher前出现了如SiteMesh这种特殊的过滤器,还必须在SiteMesh前引用Struts2的ActionContextCleanUp过滤器

对应Struts2的架构图如下



4.这时FilterDispatcher会将请求转发给ActionMapperActionMapper负责识别当前的请求是否需要Struts2做出处理。ActionMapper就类似于公司的保安,来识别是不是当前客户是不是我公司的人

对应Struts2的架构图如下



5.如果需要Struts2处理,ActionMapper会通知FilterDispatcher,需要处理这个请求,FilterDispatcher会停止过滤器链以后的部分,(这也就是为什么,FilterDispatcher应该出现在过滤器链的最后的原因)。然后建立一个ActionProxy实例,这个对象作为Action与xwork之间的中间层,会代理Action的运行过程。

对应Struts2的架构图如下



6.ActionProxy对象在被创建出来的时候,并不知道要运行哪个Action,它手里只有从FilterDispatcher中拿到的请求的URL。
而真正知道要运行哪个Action的是ConfigurationManager。因为只有它才能读取我们的strtus.xml

(在服务器启动的时候,ConfigurationManager就会把struts.xml中的所有信息读到内存里,并缓存,当ActionProxy带着URL向他询问要运行哪个Action的时候,就可以直接匹配、查找并回答了)

对应Struts2的架构图如下

  -> 

7.ActionProxy知道自己该干什么事之后(运行哪个Action、相关的拦截器以及所有可能使用的result信息),然后马上建立ActionInvocation对象了,ActionInvocation对象描述了Action运行的整个过程。

注意:Action完整的调用过程都是由ActionInvocation对象负责


对应Struts2的架构图如下




8.在execute方法之前,好像URL请求中的参数已经赋值到了Action的属性上,这就是我们的"雷锋"—拦截器。

拦截器的运行被分成两部分,一部分在Action之前运行,一部分在Result之后运行,而且顺序是刚好反过来的。也就是在Action执行前的顺序,比如是拦截器1、拦截器2、拦截器3,那么运行Result之后,再次运行拦截器的时候,顺序就变成拦截器3、拦截器2、拦截器1了。

这就好比,你要去奶奶家,需要通过 水泊梁山->盘丝洞 -> 索马里,到了奶奶家,看奶奶回来的时候,就必须要通过 索马里 -> 盘丝洞 -> 水泊梁山。

所以ActionInvocation对象执行的时候需要通过很多复杂的过程,按照指定拦截器的顺序依次执行。


对应Struts2的架构图如下





9.到了奶奶家,然后执行Action的execute方法





10.然后根据execute方法返回的结果(Result),去struts.xml中匹配选择下一个页面




11.根据结果(Result)找到页面后,在页面上(有很多Struts2提供的模板),可以通过Struts2自带的标签库来访问需要的数据,并生成最终页面

注意:这时还没有给客户端应答,只是生成了页面




12.最后,ActionInvocation对象倒序执行拦截器,从奶奶家回来




13.ActionInvocation对象执行完毕后,已经得到响应对象(HttpServletResponse)了,最后按与过滤器(Filter)配置定义相反的顺序依次经过过滤器,向客户端展示出响应的结果


得到完整Struts2架构图




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326804779&siteId=291194637