Controller: used to control the application process and process the requests sent by the view. After the Controller receives a user request, It maps the user data to the model update, that is, it calls the model to implement the user request function; then, the Controller selects the view for the response and displays the updated model data to the user.2: Relationship between models and views
In MVC, the model and view are separated. Generally, there is no logical implementation in the view, and the model does not depend on the view. The same model may have many different display methods, that is, the same model can correspond to multiple different views.
Such examples are everywhere. For example, when you browse folders on a windows operating system, the data in folders remains unchanged, but there are many ways to display them, for example, large icons, small icons, detailed information, and so on. Take the objects in the tomcat7 folder as an example and follow different display methods, as shown in Figure 1.1:
Figure 1.1 A model corresponds to multiple views
The model is responsible for the output content, while the view is responsible for the output form. The model does not depend on the view, and the model and view are decoupled. Therefore, when you modify a view, that is, the display mode, you do not have to worry about the model. Instead, you only need to directly modify the display mode of the view.
3: MVC component Diagram
MVC component relationship 1.2 is shown in:
Figure 1.2 MVC component Diagram
The component relationship diagram of MVC describes the interaction between the model, view, and controller. The interaction relationships are described in detail in the interaction sequence below:
(1) first, the view is displayed to the user. The user operates on the view and enters some business data.
(2) then the user will click the submit button to send the request
(3) The view sends a user request to the controller. The request contains the business functions and related data to be completed.
(4) The Controller processes user requests, encapsulates the data in the requests, selects and calls an appropriate model, and requests the model to update the status, then select the view to be displayed to the user.
(5) The model processes the business functions of user requests and maintains and updates the model status.
(6) When the model status changes, the model notifies the corresponding view to tell the view that its status has changed.
(7) after receiving a notification from the model, the View queries the status of the model, obtains the data to be displayed, and then displays the data according to the display method of the view.
The next step is to wait for the user to perform the next operation.
1.1.3 role of MVC ModeIn early development, some programmers did not realize the benefits brought by the MVC pattern and did not follow the MVC pattern during development. The result is that the program structure is not clearly divided, and the functions of each part are chaotic. When the business functions are changed, many classes need to be modified, whether it is the business logic modification or the display form modification, as a result, the efficiency of software development and maintenance is low, with hundreds of errors.
Following the MVC pattern to develop the system will greatly avoid the above problems. The core method of the MVC model is decoupling. The MVC model divides the entire application into three parts, namely model, view, and controller, then, the communication between the three parts is strictly controlled to obtain an application with clear structure, reasonable function distribution, reusable, scalable, and maintainable.
Therefore, the following benefits can be obtained by using the MVC mode: