SpringMVC execution principle, clear and easy to understand

Reprinted: Original link: https://blog.csdn.net/qq_43297152/article/details/106100687

What is the specific process executed by SpringMVC? If you go to the official document, the English in it really makes your scalp numb. When you click on translate, when the word spring is translated into spring, you will find that the big thing is not good. This article will explain it in the most straightforward terms. Take a look at the SpringMVC process.

 

table of Contents

 

basic understanding

The SpringMVC framework is based on the component method to execute the process. There are many different components in SpringMVC, all with their own unique functions. Just like making shoes in a factory assembly line, some parts are made of soles, and some parts are made of shoelaces, each performing its own duties.

Five components

What are the components of SpringMVC:
1, DispatcherServlet: front controller
2, HandlerMapping: processor mapper
3, HandlAdapter: processor adapter
4, Handler: processor
5, View Resolver: view resolver

With so many devices, it is really a headache to look at. Don't panic, I'll explain to you slowly below.

The role of each component

1,DispatcherServlet

Function: It
is the command center, as it accepts the request from the client, which is equivalent to a repeater,
which calls other components to process the user's request.

Vernacular: Just like a boss, he will let this department produce an airplane for him, and let that department produce a car.

2,Handler

Function:
It is the specific business controller to be written in our development to process specific user requests.

Example: The Handler processor is actually the method we write in the controller layer. For example, the following picture is a Handler processor.Insert picture description here

3 , HandlerMapping

Function:
Find the corresponding Handler processor according to the requested url. Because there are many Handler processors, when the mapping path of the Handler processor corresponds to the requested URL, it means that it is the Handler we are looking for.

SpringMVC provides different mappers, which can be implemented through different mapping methods, such as configuration files, implementation interfaces, annotations, etc. The most commonly used is annotations.

Example:
If your request url is: http://localhost:8085/stu/findAll,
he will find the corresponding Handler based on /stu/findAll. You will also find the handler processor shown below.
Insert picture description here

4 , HandlerAdapter

Role:
Because there are various Handlers, adapters are needed. HandlerAdapter is responsible for adapting parameter binding, converter, which view resolver should be called, etc. when Handle is executed.

Vernacular: He is like a shared power bank, with both an Apple data cable and an Android data cable. When you charge an Apple phone, you will definitely choose the Apple cable, and when you charge an Android phone, choose the Android cable.

5,ViewResolver

Function:
View Resolver is responsible for generating View views from the processing results. View Resolver resolves the logical view names into physical view names (specific page addresses) to generate View view objects.

Specific explanation:
1. The String, View, or ModelAndView returned in the handler (processor) will eventually be converted into a ModelAndView object. ViewResolver (view resolver) will receive this ModelAndView object.

2, ViewResolver (view resolver) will parse the received View (view name) into an InternalResourceView object, InternalResourceView will store the accepted Model in the corresponding request domain, and then forward the request to the target URL on the client through RequestDispatcher .

(What is ModelAndView, it is a class, you can set the view name and Model you want to return. Model can encapsulate data.)

ModelAndView modelAndView = new ModelAndView();
        //设置视图名称
        modelAndView.setViewName();
        //设置你要封装的数据
        modelAndView.addObject();
  • 1
  • 2
  • 3
  • 4
  • 5

The following is that I did not use the ModelAndView class, directly wrote a Model, and returned a String, but the Handler (processor) will convert it into a ModelAndView object and return it to other components:
Insert picture description here
View Resolver View resolver configuration:
Insert picture description here
according to The view name in the handler, and the suffix and package name configured by the ViewResolver view resolver form the target URL we want to forward: http://localhost:8085/list.jsp.

We look at the picture below, because it is forwarding, the url will not change, but the page has become a list.jsp page
Insert picture description here
Insert picture description here

Overall process

First on the picture, this is the process executed by SpringMVC.
Insert picture description here

1. DispatcherServlet (front controller) accepts the request.

2. DispatcherServlet (front controller) calls HandlerMapping (processor mapper), and finds the corresponding Handler (processor) according to the url of the request.

3. HandlerMapping (processor mapper) returns the found Handler (processor) to DispatcherServlet (front-end processor).

4. DispatcherServlet (front controller) calls HandlAdapter (processor adapter) to let it execute Handler (processor).

5. HandlAdapter (processor adapter) executes Handler (processor).

6, Handler (processor) will return ModelAndView to HandlAdapter (processor adapter).

7. HandlAdapter (processor adapter) returns the ModelAndView obtained to DispatcherServlet (front-end processor).

8. DispatcherServlet (front controller) calls ViewResolver (view resolver) to let it resolve the logical view name in ModelAndView, that is, resolve the view name in ModelAndView.

9. View Resolver (view resolver) returns a specific view to DispatcherServlet (front-end processor).

10. DispatcherServlet (front-end processor) renders the view, that is, fills the Model (model data) in ModelAndView into the view.

11. DispatcherServlet (front-end processor) responds to users.


If there are any errors or irregularities, please point them out. Of course, if you have any questions or do not understand, you can raise them in the comments below. If it helps you, please give me a thumbs up.

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/109676789