How spring MVC works

Time:2020-10-29

Working principle diagram of spring MVC:

Spring MVC process

1. The user sends a request to the front-end controller, dispatcherservlet.

2. The dispatcher servlet receives a request to call the handlermapping processor mapper.

3. The processor mapper finds the specific processor (which can be found according to the XML configuration and annotation), generates the processor object and returns the processor interceptor (if any) to the dispatcher servlet.

4. The dispatcher servlet calls the handleradapter processor adapter.

5. Handleradapter calls the specific processor (controller, also called back-end controller) through adaptation.

6. The controller returns modelandview after execution.

7. The handleradapter returns modelandview, the result of controller execution, to the dispatcher servlet.

8. The dispatcher servlet passes modelandview to the viewreslver view parser.

9. After the viewresrover is parsed, the specific view is returned.

10. The dispatcher servlet renders the view based on the view (that is, the model data is filled into the view).

11. The dispatcher servlet responds to the user.

Component description:

The following components are typically implemented using frameworks:

Dispatcher servlet: as the front-end controller, the center of the whole process control, controls the execution of other components, unifies the scheduling, reduces the coupling between components, and improves the scalability of each component.

Handlermapping: implement different mapping methods by extending the processor mapper, such as configuration file mode, implementation interface mode, annotation mode, etc.

Handladapter: support more types of processors by extending the processor adapter.

Viewresolver: by extending the view parser, it supports more types of view parsing, such as JSP, FreeMarker, PDF, Excel, etc.

Components:
1. The front-end controller, dispatcherservlet, is provided by the framework
Function: receive request, response result, equivalent to transponder, CPU. With the dispatcher servlet, the coupling between other components is reduced.
When the user request arrives at the front-end controller, it is equivalent to C in MVC mode. The dispatcher servlet is the center of the whole process control, and it calls other components to process the user’s request. The existence of dispatcher servlet reduces the coupling between components.

2. The processor mapper handlermapping (no need for engineer development) is provided by the framework
Function: find handler according to the requested URL
Handlermapping is responsible for finding handler or processor according to user’s request. Spring MVC provides different mappers to implement different mapping methods, such as configuration file mode, implementation interface mode, annotation method, etc.

3. Processor adapter handleradapter
Function: execute handler according to specific rules (rules required by handleradapter)
The processor is executed through handleradapter, which is the application of adapter mode. More types of processors can be executed by extending the adapter.

4. Processor Handler (need Engineer Development)
Note: when writing a handler, follow the requirements of handleradapter, so that the adapter can execute handler correctly
Handler is the back-end controller of the front-end controller of the dispatcher servlet. Under the control of the dispatcher servlet, the handler processes the specific user requests.
Since handler involves specific user business request, engineers usually need to develop handler according to business requirements.

5. View resolver (not required to be developed by engineers) is provided by the framework
Function: to parse the view and convert it into a real view according to the logical view name
The view resolver is responsible for generating the view view from the processing results. The view resolver first resolves the logical view name into the physical view name, that is, the specific page address, and then generates the view view object. Finally, the view is rendered and the processing result is displayed to the user through the page. Spring MVC framework provides many view types, including jstlview, freemarkerview, pdfview, etc.
In general, we need to display the model data to users through the page tag or page template technology, and we need engineers to develop specific pages according to the business needs.

6. View (need engineer to develop JSP…)
View is an interface, and the implementation class supports different view types (JSP, FreeMarker, PDF…)

The specific process steps of the core architecture are as follows:
1. First of all, the user sends a request – > dispatcher servlet. After receiving the request, the front-end controller does not process the request itself, but entrusts it to other parsers for processing. As a unified access point, it controls the global process;
2. Dispatcherserservlet > handlermapping. Handlermapping will map requests to handlerexecutionchain objects (including a handler processor (page controller) object and multiple handlerinterceptor interceptors). Through this policy pattern, it is easy to add new mapping strategies;
3. Dispatcher servlet — > handleradapter, which will package the processor as an adapter, so as to support the application of various types of processors, that is, the application of adapter design pattern, so it is easy to support many types of processors;
4. Handleradapter > call the function processing method of the processor. Handleradapter will call the function processing method of the real processor according to the adaptation results to complete the function processing, and return a modelandview object (including model data and logical view name);
5. The logical view name of modelandview is > viewresolver. Viewresolver will resolve the logical view name into a specific view. Through this strategy mode, it is easy to replace other view technologies;
6. View > rendering. View will render according to the model data transferred in. The model here is actually a map data structure, so it is easy to support other view technologies;
7. Return the control right to the dispatcher servlet, and the dispatcher servlet returns the response to the user, and the process ends.

The following two components usually need to be developed:

Handler: the processor, that is, the back-end controller, is represented by controller.

View: view, that is, the interface displayed to the user. In view, tag language is usually required to display model data.

 

Before we introduce spring MVC, let’s take a look at what MVC patterns are

MVC:MVC is a design pattern

MVC schematic diagram:

analysis:

M-model model (complete business logic: composed of JavaBean, service + Dao + entity)

V-view view (display the interface JSP, HTML…)

C-controller controller (receive request > call model > dispatch page according to result)

 

What is spring MVC 

  Spring MVC is an open source framework of MVC. Spring MVC = struts 2 + spring. Spring MVC is equivalent to the integration of struts 2 and SRG. However, there is a puzzle here: what is the relationship between spring MVC and spring? There is a good explanation on Baidu Encyclopedia: it means that spring MVC is a follow-up product of spring. In fact, spring provides MVC module of Web Application on the original basis. Spring MVC can be simply understood as a module of spring (such as AOP and IOC). On the network, it is often said that spring MVC and spring are seamlessly integrated, in fact, springm VC is a sub module of spring, so there is no need to integrate with spring.

Schematic diagram of spring MVC:

You may have a lot of doubts when you see this diagram. Now let’s take a look at the steps of this diagram: (you can compare the schematic diagram of MVC for understanding)

Step 1: the user initiates a request to the front-end controller (dispatcher servlet)

Step 2: the front-end controller requests the handler mapper to find the handle: search through XML configuration or annotation

Step 3: find the handler mapper and return to the handler execution chain like the front-end controller

Step 4: the dispatcher servlet calls the handler adapter to execute the handler

Step 5: the processor adapter executes the handler

Step 6: the handler returns modelandview to the processor adapter after execution

Step 7: the processor adapter returns modelandview to the front controller

Step 8: the front controller requests the view resolver to parse the view

Step 9: the view parser returns view like the front controller

Step 10: the front controller renders the view

Step 11: the front-end controller responds to the user

Seeing these steps, I believe you are very confused. This is normal, but here is to let you understand several components in spring MVC

Front end controller (dispatcher servlet): receives requests and responds to results, equivalent to the CPU of the computer.

Handler mapping: find the processor according to the URL

Handler: a programmer is required to write code to process logic

Handler adapter: the processor will be packaged as an adapter, so that it can support various types of processors, similar to the adapter of notebook (Application of adapter mode)

View resolver (viewresovler): for view parsing, multiple returned strings are processed, and can be parsed into corresponding pages