This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

What is MVC?

The MVC pattern is a common design pattern in our development, and each letter stands for an abbreviation of a module. MVC is an acronym for three words: Model, View, and Controller. In the face of today’s complex business scenarios, we usually mix MVC with a variety of modes, so that the project architecture and modules inside the service can be more fine-grained, and the maintainability and extensibility can be enhanced.

Views in MVC

A View represents a user interface. For Web applications, it can be generalized as an HTML interface, but it can be XHTML, XML, or applets. As applications grow in complexity and scale, handling interfaces becomes challenging. An application may have many different views, and the MVC design pattern is limited to the collection and processing of data on the view and the user’s request, excluding the processing of business processes on the view. The processing of the business process is handed over to the Model. For example, a view of an order only accepts data from the model and displays it to the user, and passes user interface input data and requests to the control and model.

Models in MVC

Model: The processing of business processes/states and the formulation of business rules. The processing of the business process is a black box operation for the other layers, with the model accepting the data requested by the view and returning the final processing result. The design of the business model is arguably the core of MVC. The popular EJB model is a typical example of an application that further divides the model from an application technology implementation perspective to take advantage of existing components, but it cannot be used as a framework for an application design model. It simply tells you that certain technical components can be exploited by following this model, thereby reducing the technical difficulties. For a developer, it is possible to focus on the design of the business model. MVC design pattern tells us that the application model is extracted according to certain rules, and the level of extraction is very important, which is also the basis for judging whether a developer is a good designer. Abstract and concrete can not be separated too far, also can not be too close. MVC doesn’t provide a way to design your models, it just tells you that you should organize your models to facilitate refactoring and reuse. We can use the object programming analogy, where MVC defines a top-level class and tells its subclasses that you can only do this, but there’s no limit to what you can do. This is very important for programmers.

Another important business model is the data model. Data model mainly refers to the data preservation (persistence) of entity objects. For example, save an order to the database and retrieve the order from the database. We can list this model separately and restrict all database-related operations to this model.

Controllers in MVC

The Controller can be understood as receiving requests from the user, matching the model with the view, and completing the user’s requests together. The function of the partition control layer is also obvious, it clearly tells you that it is a distributor, what model to choose, what view to choose, what user requests can be completed. The control layer does not do any data processing. For example, when a user clicks on a connection and the control layer accepts the request, it does not process business information. It only passes the user’s information to the model, tells the model what to do, and selects the view that meets the requirements and returns it to the user. Thus, one model may correspond to multiple views, and one view may correspond to multiple models.

The separation of model, view, and controller enables a model to have multiple display views. If the user changes the model’s data through one view’s controller, all other views that depend on that data should reflect those changes. Therefore, whenever any data changes occur, the controller notifies all views of the change, resulting in an update of the display. This is actually a model variable-propagation mechanism. The relationship between model, View and controller and their main functions: 1) The top layer is the “View layer” directly facing the end user. It is provided to the user’s operation interface, is the shell of the program. 2) The bottom layer is the core “Data layer” (Model), which is the data or information that the program needs to manipulate. 3) The middle layer is the “Controller”, which is responsible for selecting the data in the “data layer” according to the user’s input instructions from the “view layer”, and then performing corresponding operations to produce the final result.

The three layers are closely linked but independent of each other, and changes within each layer do not affect the other layers. Each layer provides an Interface that can be invoked by the upper layer. In this way, the software can be modular, changing the look and feel or changing the data without changing other layers, greatly facilitating maintenance and upgrades.

Spring MVC principles

Interactive flow chart

The interactive process is shown in the figure below:

Description of the process

Process description: (1) The client (browser) sends requests directly to DispatcherServlet. (2) The DispatcherServlet invokes HandlerMapping according to the request information to parse the Handler corresponding to the request. (3) After parsing to the corresponding Handler, it is processed by the HandlerAdapter adapter. (4) The HandlerAdapter will call the real processor to process the request and process the corresponding business logic according to the Handler. (5) After processing the business, the processor will return a ModelAndView object, Model is the returned data object, View is a logical View. (6) ViewResolver will find the actual View according to the logical View. (7) DispaterServlet passes the returned Model to the View. (8) Return to the requester via View (browser)

Reference documentation

  • zhuanlan.zhihu.com/p/322316709
  • Blog.csdn.net/yanweihpu/a…