Gain a deep understanding of Struts1's Operating Mechanism
To talk about the workflow of Struts1, you must talk about Model1 and Model2. This framework is developed step by step with their bodies.
Model1 development mode. When we first started to access Java, we used this mode. A jsp page + DaoBean that processes business logic JavaBean + DaoBean that processes data. In addition, you can directly connect to the database to obtain data. It is very simple, easy to understand, and the development speed is fast enough, there is no tedious conversion, it is very easy to get started, so it is a good choice to do a small project and use the development mode of Model1. But let's imagine that if we say that the Jsp page, the Minutes are responsible for displaying and the business logic, we can imagine what we will do if we want to change it! Another problem is that program logic development and page design are entangled, which neither facilitates division of labor and cooperation nor facilitates code reuse. Such a program has poor robustness and scalability.
Model 2 introduces the concept of "controller". Generally, the controller is served by Servlet. client requests are not directly sent to a JSP page that processes business logic, but to the Controller, then, the Controller calls different transaction logic based on the specific request and returns the processing result to the appropriate page. Therefore, this servlet Controller provides an application with a hub for front-and back-end processing. On the one hand, it provides an appropriate entry point for the verification, identity authentication, logs, and international programming of input data; on the other hand, it also provides the possibility of separating the business logic from the JSP file. After the business logic is separated from the JSP page, the JSP file is transformed into a View that simply completes the display task. The independent transaction logic becomes the Model that people often say, and the controller Control itself constitutes the MVC mode. Practice has proved that the MVC mode provides great convenience for the development and maintenance of large programs.
Struts1 can be said to be an enhanced version of Model2. All requests from customers who need to pass the framework are received by ActionServlet in a unified manner (ActionServlet Struts has already been written for us, as long as your application has no special requirements, it basically meets your requirements), according to the received request parameters and Struts configuration (struts-config.xml) in ActionMapping, send the request to the appropriate Action to solve the problem of who made it. They constitute the Struts controller. Action is the component that actually works in the Struts application. It solves the problem of what to do. It calls the required business component (model) to complete the application business, the business component solves the problem and returns the execution result to an ActionForward object of the JSP (or Action) that represents the desired response to present the response to the customer. Here, we should note that the Action class should not contain too many business logic, but should simply collect the data required by the Business Method and pass it to the business object. In fact, its main responsibilities are to verify the prerequisites or declare the business logic methods required for calling to detect or handle other error routes and control them to the relevant view.
Let's take a look at the working process schematic of Struts1!
I believe that by looking at a picture and adding a description of the front, we can clearly understand the working process and running mechanism of Struts1! Then I want to tell you some things to pay attention.
ActionServlet uses the process () method to process all logic.
Aaction configuration information is not all loaded, but the action configuration information corresponding to the intercepted url is found and loaded into ActionMapping, that is, an action corresponds to an ActionMapping.
ActionForm does not reflect every time, but first checks whether the request or session has been created. If not, it is created once and saved in the request or session.
The Action processing class also checks whether it has been created before the operation. In addition, the Action creation process in struts1 has thread security issues.
The ActionServlet calls processForwardConfig Based on the ActionForward returned by the Action to perform page navigation.