The execution principle of springMVC

SpringMVC is a web framework implemented based on Servlet API, and its execution process can be roughly divided into the following steps:

  1. When a user sends a request, the request will first be received by the DispatcherServlet (front controller).
  2. DispatcherServlet (front controller) will determine which Controller to use to process the request based on the URL in the request.
  3. According to the HandlerMapping (processing mapper) in the configuration file, DispatcherServlet (front controller) forwards the request to the corresponding Controller.
  4. The Controller will process the request according to the business logic and return the result to the DispatcherServlet (front controller).
  5. DispatcherServlet (front controller) will call ViewResolver (view resolver) to find the corresponding View.
  6. View will render the result and return it to DispatcherServlet (front controller).
  7. DispatcherServlet (front controller) returns the result to the client.

Specifically, the execution flow of SpringMVC is as follows:

  1. The client sends an HTTP request to the server.
  2. After the server receives the request, it will be handed over to DispatcherServlet (front controller) for processing.
  3. DispatcherServlet (front controller) selects the appropriate Controller through HandlerMapping (processing mapper) according to the request information.
  4. The Controller returns the ModelAndView (model and view) object after processing the request.
  5. DispatcherServlet (front controller) finds the corresponding View through ViewResolver (view resolver).
  6. View renders ModelAndView (model and view) into HTML pages.
  7. The HTML page is returned to the client as the response content.

In this process, it should be noted that when the DispatcherServlet is initialized, the configuration file of SpringMVC will be loaded, which includes the configuration information of HandlerMapping and ViewResolver. These configuration information will be used to route requests and parse response data. At the same time, you also need to pay attention to the use of the RequestMapping annotation, which can specify the corresponding relationship between the URL and the Controller, so as to handle different requests more flexibly.

Guess you like

Origin blog.csdn.net/weixin_60415789/article/details/130510037