Preface
Struts2 is a relatively popular MVC framework. Before we say Struts2, let's review the traditional MVC architecture of SERVLET/JSP implementations: JSP page (view) sends messages to Servlet (Controller controller), servlet gets request data, handles business logic , distribution turned. In this case, the servlet is too bloated, what work is the servlet in the dry, and the business logic is all hard coding, the code is written dead is not conducive to maintenance.
The MVC of the STRUTS2 framework corresponds to the respective:
V:jsp+ognl
C:action class
M:javabean+modeldriven
The controller of the STRUTS2 framework extracts the "Get Requests" and "Distribute steering" code out of the configuration file so that the Controller (action Class) can focus on the processing of the business logic. Body
1, a complete STRUTS2 Project Web directory structure and the implementation process of the main files
File Execution Process relationships
As can be seen from the above diagram, STRUTS2 's MVC parts are all around the Struts.xml file, which frees the controller's burden and solves the problem of hard coding.
Internal execution process of the 2,STRUTS2 framework
It says the execution of a file in MVC, and that's just a small part of it, now look at the implementation mechanism of the entire STRUTS2 framework, the following figure comes from the official website, and then adds its own changes.
As the above figure shows, the entire framework of the operation and tightly around the core filter strutsprepareandexecutefilter start work. Digging into the source code of the filter will help in understanding. The following several blogs on the introduction of filter is very good.
(1), STRUTS2 request process Source Analysis: Blog 1
(2), Struts 2 config file loading order: Blog 2
3, Default Interceptor
A default interceptor stack is defined in the Struts-default.xml configuration file, which is what is performed before the action method executes. Commonly used are encapsulated user form data to the JavaBean Modeldriven Interceptor, for input validation of the validation interceptor, and so on.
The interaction between 4,view and controller
The user requests from the view page generate some data, where they are stored. In fact, every time the action class executes, the core filter Strutsprepareandexecutefilter creates 2 objects: Actioncontext and Valuestack, which store all the data used during an action visit. This data can also be obtained by strut tags and ognl expressions on the JSP page.
(1), Actioncontext is a map data structure, where the key is some common domain objects (application,session,request, etc.), and value is a map. That is to say Actioncontext is a large map wrapped around some small map.
(2), Valuestack is a ArrayList data structure, and is a stack structure, each time on the top of the stack access to data.
The interaction between 5,controller and model
The interaction between C and M is relatively simple, using the interceptor provided by the Struts framework: Modeldriven to encapsulate the data submitted by the user form into the corresponding JavaBean. Main points: (1) JavaBean class to write their own. (2) The action class is to implement the Modeldriven interface. (3) Implement abstract method Getmodel (). Summary
In this paper, the basic structure of the STRUTS2 framework is interpreted from a macroscopic perspective, and the specific details of each part are explained later.