The principle of struts2

As we all know, Struts2 is a very good open source framework. We can use the Struts2 framework for development, and we can quickly build a Struts2 framework, but whether we can express the working principle of the Struts2 framework clearly in language, the principle you express does not need to be stated. How the bottom layer is implemented, I guess most people don't understand it. When an interviewer asks us how Struts2 works, how do we answer that question? Answer what can make your speech different, first look at the summary of other friends

 
Note: Reference article: http://www.blogjava.net/GavinMiao/archive/2011/08/29/357480.html
One working principle
The processing in the Struts2 framework is roughly divided into the following steps 
: 1. The client initiates a request to the Servlet container (such as Tomcat) 
2. This request goes through a series of filters (one of these filters is called ActionContextCleanUp. Select filter, this filter is very helpful for the integration of Struts2 and other frameworks, such as: SiteMesh Plugin) 
3 Then FilterDispatcher is called, FilterDispatcher asks ActionMapper to decide whether to call an Action 
4 If ActionMapper decides to call a certain Action, FilterDispatcher hand over the processing of the request to ActionProxy 
5 ActionProxy asks the configuration file of the framework through Configuration Manager, finds the Action class that needs to be called 
6 ActionProxy creates an instance of ActionInvocation. 
7 The ActionInvocation instance is called using the naming pattern. Before and after the process of invoking the Action, it involves the invocation of the relevant interceptor (Intercepter). 
8 Once the Action is executed, ActionInvocation is responsible for finding the corresponding return result according to the configuration in struts.xml. The returned result is usually (but not always, another Action chain) a JSP or FreeMarker template that needs to be represented. Labels inherited from the Struts2 framework can be used during presentation. ActionMapper needs to be involved in this process 
 
Two workflow
1. The client browser sends an HTTP request.
2. According to the web.xml configuration, the request is received by FilterDispatcher
3. According to the struts.xml configuration, find the Action class and method to be called, and inject the value into Aciton through the IoC method
4. Action calls the business logic component to process business logic. This step includes form validation.
5. After the Action is executed, find the corresponding return result according to the configuration in struts.xml, and jump to the corresponding page
6. Return an HTTP response to the client browser
 
Seeing the netizens' summary of the principle of Struts2, I also summarize it for the follow-up interview. The following are my questions
1. The customer service side initiates a request and points to the Tomcat container through the HTTP protocol. What did she do when tomcat got the request?
2. Our web.xml configuration
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
 <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>
We can see org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter from the web configuration file. What is the use of this Filter class?
This  /*  is to intercept all requests, what did he do when he intercepted the request? <url-pattern>/*</url-pattern>
3.我们struts.xml配置
 
<struts>
 <package name="default" namespace="/" extends="struts-default">
        
        <action name="hello">
            <result>
                /Hello.jsp
            </result>
        </action>
        
    </package>
 
</struts>
谁负责根据struts.xml配置,找到需要调用的Action类和方法呢?
 
 
4。最后根据struts.xml中的配置找到对应的返回结果result,在返回HTTP响应到客户端浏览器

结束语 我觉的要说清楚Struts原理,不光死记硬背资料上的写的原理,还要带着这些问题去做总结。

Guess you like

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