Home  >  Article  >  Java  >  A brief introduction to the working principle of Spring mvc

A brief introduction to the working principle of Spring mvc

巴扎黑
巴扎黑Original
2017-09-08 09:40:042375browse

This article mainly introduces relevant information on the working principle of Spring mvc in detail, which has certain reference value. Interested friends can refer to it

SpringMVC Framework Introduction

Spring MVC is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow.
The Spring framework provides full-featured MVC modules for building web applications. Using Spring's pluggable MVC architecture, you can choose whether to use the built-in Spring Web framework or a Web framework such as Struts. The Spring Framework is highly configurable through policy interfaces and includes multiple view technologies such as JavaServer Pages (JSP) technology, Velocity, Tiles, iText, and POI. The Spring MVC framework is unaware of the views used, so it does not force you to use only JSP technology.

              Spring MVC separates the roles of controllers, model objects, dispatchers, and handler objects. This separation makes them easier to customize.

Spring's MVC framework is mainly composed of DispatcherServlet, processor mapping, processor (controller), view resolver, and view.

SpringMVC schematic diagram

SpringMVC interface explanation

DispatcherServlet interface:

The front-end controller provided by Spring, through which all requests are uniformly distributed. Before DispatcherServlet distributes the request to Spring Controller, it needs to use the HandlerMapping provided by Spring to locate the specific Controller.

HandlerMapping interface:

Can complete the mapping of customer requests to Controller.

Controller interface:

Needs to handle the above requests for concurrent users, so when implementing the Controller interface, it must be ensured Thread safe and reusable.
Controller will handle user requests, which is consistent with the role played by Struts Action. Once the Controller has processed the user request, it returns the ModelAndView object to the DispatcherServlet front-end controller. ModelAndView contains the model (Model) and the view (View).
From a macro perspective, DispatcherServlet is the controller of the entire Web application; from a micro perspective, Controller is the controller in the processing of a single Http request, and ModelAndView is the model (Model) and view (View) returned in the Http request process. ).

ViewResolver interface:

The view resolver (ViewResolver) provided by Spring finds View objects in web applications , thereby rendering the corresponding results to the client.

SpringMVC operating principle

1. The client request is submitted to the DispatcherServlet
2. The DispatcherServlet controller queries one or more HandlerMappings and finds the processing Requested Controller
3. DispatcherServlet submits the request to Controller
4. After Controller calls business logic processing, it returns to ModelAndView
5. DispatcherServlet queries one or more ViewResoler view parsers to find the view specified by ModelAndView
6. The view is responsible for displaying the results to the client

DispatcherServlet is the core of the entire Spring MVC. It is responsible for receiving HTTP requests, organizing and coordinating the various components of Spring MVC. Its main tasks are as follows:

1. Intercept URL requests that match a specific format.
2. Initialize the WebApplicationContext corresponding to the DispatcherServlet context and associate it with the WebApplicationContext of the business layer and persistence layer.
3. Initialize each component of Spring MVC and assemble it into DispatcherServlet.

The above is the detailed content of A brief introduction to the working principle of Spring mvc. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn