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:用来处理请求,封装数据。
- 二、运行流程
注意:Action完整的调用过程都是由ActionInvocation对象负责
拦截器的运行被分成两部分,一部分在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架构图