Five core components and calling process of spring MVC

Time:2021-6-27

Five core components of spring web MVC

The dispatcher servlet controller entry is responsible for distributing requests
Handlermapping is responsible for finding the corresponding controller according to the request
The controller that actually handles the request
Modelandview encapsulates data information and view information
The viewresolver view processor finds the corresponding page through processing

DispatcherServlet

Dispatcher servlet is a front-end controller, which is configured in web.xml file. To intercept matching requests, servlet needs to define its own interception matching rules. Distributing the intercepted requests to the target controller for processing according to the corresponding rules is the first step in configuring spring MVC.

Dispatcherservlet is the implementation of front-end controller design pattern. It provides the centralized access point of spring web MVC, and is responsible for the assignment of responsibilities. It is also seamlessly integrated with spring IOC container, so that all the benefits of spring can be obtained.

HandlerMapping 

Handlermapping is a processor mapping, which is used to dispatch requests and establish corresponding relationship between requests and controllers. It is called by the dispatcherservlet, which takes all data from the container   Handlermapping instance and traversal, let   The handlermapping instance tries to find the handler according to the way it implements the class. That is to say, the dispatcher servlet needs to consult a bean whose name is “handlermapping” to give a request to a specific controller.

Controller

The controller is responsible for processing the data sent by the dispatcher servlet   It encapsulates the data requested by users into a model after being processed by the business processing layer  , And then the model   Return to the corresponding view   Show it.

ModelAndView                 

The model encapsulates data information and view information. The modelandview class is used to store the processed result data and the view to display the data. From the name, the model in modelandview represents the model, and the view represents the view. This name explains the function of this class very well. After calling the model layer to process the user’s request, the business processor stores the result data in the model attribute of the class, stores the view information to be returned in the view attribute of the class, and then makes the modelandview return to the spring   MVC framework. The framework parses the object by calling the view parser defined in the configuration file, and finally displays the result data on the specified page.

ViewResolver                   

View resolver   The main function of is to resolve a logical view name to a real view.

The relationship between the five components

After receiving the request, the dispatcher servlet calls the corresponding controller according to the configuration of handlermapping. The controller encapsulates the processing result into modelandview object and returns it to the dispatcher servlet. According to the analysis of viewresolver, the dispatcher servlet calls the corresponding view object (such as JSP) to generate the corresponding page.
In a word, the request is sent to the central controller — to the mapping processor — to the specified controller — to obtain the data and view to form a modelandview component — to return to a specific foreground page through viewresolver.
Five core components and calling process of spring MVC
Five core components and calling process of spring MVC

Spring MVC principle
When a request arrives, the first front-end controller to accept the request is called dispatcher servlet (this needs to be configured in web. XML), and the back-end controller is called controller. Handle the mapping of request URL and back-end controller is called handmapping. It has many types and is flexible. It is also configured on an XML file. The model object responsible for business logic processing is also the Dao / dto component we usually write. The controller returns a modelandview object to the dispatcher servlet. Modelandview can carry a view object or a logical name of the view object. If you are carrying the logical name of a view object, the dispatcher servlet needs a viewresolver to find the view object used to render the response. Finally, the dispatcher servlet dispatches the request to the view object specified by the modelandview object. The view object is responsible for rendering the response back to the client.
The more specific workflow is as follows:
① When a user clicks a link or submits a form in the browser, a request will be generated. When the request leaves the browser, it will carry the information requested by the user (such as the requested URL information, user name, password, etc.).
② The first stop of the request is spring’s dispatcher servlet, which is a front-end controller. Its job is to delegate the user’s request to other components (here, the spring MVC controller) for processing. Here, the dispatcher servlet needs to decide which controller to transfer the request to for processing, and then the handler mapping is needed. The processor map looks at the URL information of the request and then decides which controller to send the request to. For example, there are two controllers, controllerA and controllerb, which handle the requests sent by the suffix. HTML and. JSP respectively. When the requester’s suffix is. HTML, the dispatcher servlet will send the request to controllerA for processing.
③ When a suitable controller is selected, the dispatcher servlet will send the request to the controller for processing. On this controller, the user’s request will send some information submitted by the user to the controller for processing and waiting. However, the well-designed controller itself does little or no processing to the information, but hands over the business logic to one or more server objects (models) for processing.
④ When the controller processes the information carried by the user’s request (or gives it to the model layer for processing), it often produces some other data that needs to be returned to the browser for display. It is obviously unfriendly to display the raw data directly, so the view is needed to display the data. The last thing for the controller is to package the model data and specify the specific name of the view that produces the output. Then it sends the model, the view name, and the request request to the dispatcher servlet. Therefore, the controller is not coupled with the view, because the view name passed to the dispatcher servlet is not a specified special file name (for example, the suffix must be JSP or some other name), as long as it is a logical name that can produce output and display results.
⑤ The dispatcher servlet will make a request to a view resolver, which can map the logical view name to a specific view display file.
⑥ Now the dispatcher servlet knows which view file can display the results. The view will use template data to generate output, which will be returned to the client for display through the response object.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]