SpringMVC~ How it works

Introduction to SpringMVC Framework

  • 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. With Spring's pluggable MVC architecture , you can choose whether to use the built-in Spring Web framework or a Web framework like Struts. The Spring Framework is highly configurable through the strategy interface and includes multiple view technologies such as JavaServer Pages (JSP) technology, Velocity, Tiles, iText, and POI. The Spring MVC framework doesn't know which views to use, so it doesn't force you to use only JSP technology.

        Spring MVC separates the roles of controller, model object, dispatcher, and handler object, which makes them easier to customize.

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

 

SpringMVC schematic

 

 

SpringMVC interface explained

 

  • DispatcherServlet interface:     
       The front controller provided by Spring, through which all requests are distributed uniformly. Before the DispatcherServlet distributes the request to the Spring Controller, it needs to locate the specific Controller with the help of the HandlerMapping provided by Spring.
  • HandlerMapping interface:
         Able to complete client request to Controller mapping.

  • Controller interface:       

The above requests need to be handled for concurrent users, so when implementing the Controller interface, it must be thread-safe and reusable.

Controller will handle user requests, which is consistent with the role played by Struts Action. Once the Controller finishes processing the user request, it returns the ModelAndView object to the DispatcherServlet front controller. ModelAndView contains the Model and 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 during the Http request process.

  • ViewResolver interface:

The view resolver (ViewResolver) provided by Spring finds the View object in the web application and renders the corresponding result to the client.

 

How SpringMVC works

  1. Client requests are submitted to DispatcherServlet
  2. One or more HandlerMappings are queried by the DispatcherServlet controller to find the Controller that handles the request
  3. DispatcherServlet submits request to Controller
  4. After the Controller calls the business logic processing, it returns to ModelAndView
  5. DispatcherServlet queries one or more ViewResoler view resolvers 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 whole Spring MVC. It is responsible for receiving HTTP requests to organize and coordinate the various components of Spring MVC. Its main tasks are as follows:
    1. Intercept URL requests that conform to 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 the various components of Spring MVC and assemble them into DispatcherServlet.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611301&siteId=291194637