[2] What is SpringMVC [The principle of execution is very important]

2. What is SpringMVC

2.1. Overview

Insert picture description here

Spring MVC is a part of Spring Framework. It is a lightweight web framework that implements MVC based on Java. Spring MVC is still a servlet.

View official documents: https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/web.html#spring-web

Why should we learn SpringMVC?

Features of Spring MVC:

  1. Lightweight, easy to learn
  2. Efficient, request response based MVC framework
  3. Good compatibility with Spring, seamless integration
  4. Convention is better than configuration
  5. Powerful functions: RESTful, data validation, formatting, localization, themes, etc.
  6. Simple and flexible

Spring's web framework is designed around DispatcherServlet [dispatch Servlet].

The role of DispatcherServlet is to distribute requests to different processors. Starting from Spring 2.5, users who use Java 5 or above can use annotation-based development, which is very concise;

It is because SpringMVC is good, simple, convenient, easy to learn, and seamlessly integrated with Spring (using SpringIoC and Aop), using conventions is better than configuration. It can perform simple junit tests. Supports Restful style. Exception handling, localization, internationalization, Data validation, type conversion, interceptors, etc... so we have to learn.

The most important point is that there are more people and companies.

2.2, central controller

Spring's web framework is designed around DispatcherServlet. The role of DispatcherServlet is to distribute requests to different processors. Starting from Spring 2.5, users using Java 5 or above can use the annotation-based controller declaration method.

The Spring MVC framework, like many other MVC frameworks, is request-driven , dispatching requests and providing other functions around a central Servlet . DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class) .

Insert picture description here

The principle of SpringMVC is shown in the following figure:

​ When the request is initiated, the request is intercepted by the front controller, and the proxy request is generated according to the request parameters, and the actual controller corresponding to the request is found. The controller processes the request, creates the data model, accesses the database, and responds to the central controller. The controller uses the model and the view to render the view result, returns the result to the central controller, and then returns the result to the requester.

Insert picture description here

2.3, SpringMVC execution principle diagram [important]

Insert picture description here

The picture shows a relatively complete flow chart of SpringMVC. The solid line represents the technology provided by the SpringMVC framework and does not need to be implemented by the developer, and the dotted line represents that it needs to be implemented by the developer.

Briefly analyze the execution process

  1. DispatcherServlet represents the front controller and is the control center of the entire SpringMVC. The user sends a request, and the DispatcherServlet receives the request and intercepts the request.

    We assume that the requested url is: http://localhost:8080/SpringMVC/hello

    The above URL is split into three parts:

    http://localhost:8080 server domain name

    SpringMVC deployed on the web site on the server

    hello means controller

    Through analysis, the above URL is expressed as: requesting the hello controller of the SpringMVC site on the server localhost:8080.

  2. HandlerMapping is the processor mapping. DispatcherServlet calls HandlerMapping, HandlerMapping finds Handler according to the request url.

  3. HandlerExecution represents a specific Handler, its main function is to find the controller according to the url, as the above url is searched for the controller: hello.

  4. HandlerExecution passes the parsed information to DispatcherServlet, such as parsing controller mapping and so on.

  5. HandlerAdapter represents the processor adapter, which executes the Handler according to specific rules.

  6. Handler lets specific Controller execute.

  7. Controller returns specific execution information to HandlerAdapter, such as ModelAndView.

  8. HandlerAdapter passes the view logical name or model to DispatcherServlet.

  9. DispatcherServlet calls the view resolver (ViewResolver) to resolve the logical view name passed by the HandlerAdapter.

  10. The view resolver passes the resolved logical view name to DispatcherServlet.

  11. DispatcherServlet calls specific views based on the view results parsed by the view parser.

  12. The final view is presented to the user.

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/111013380