SpringMvc study notes 1-what is SpringMVC (principle)

Article quoted from Crazy God's Notes

What is SpringMVC

Spring MVC is a part of Spring Framework and is a lightweight web framework that implements MVC based on Java.
View official documents

Why should we learn SpringMVC?

Features of Spring MVC:

  • Lightweight, easy to learn

  • Efficient, request response based MVC framework

  • Good compatibility with Spring, seamless integration

  • Convention is better than configuration

  • Powerful functions: RESTful, data validation, formatting, localization, themes, etc.

  • Simple and flexible
    Spring 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 develop in the form of annotations, 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 testing. Supports Restful style. Exception handling, localization, internationalization, Data validation, type conversion, interceptors, etc... so we have to learn

Central controller

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:

当发起请求时被前置的控制器拦截到请求,根据请求参数生成代理请求,
找到请求对应的实际控制器,控制器处理请求,创建数据模型,访问数据库,
将模型响应给中心控制器,控制器使用模型与视图渲染视图结果
,将结果返回给中心控制器,再将结果返回给请求者。

Insert picture description here

SpringMVC execution principle

Insert picture description here

用户发起请求,请求会经过一个前端控制 DispatcherServlet,DispatcherServlet根据请求到找到处理映射
器 HandleMapper,HandleMapper根据url会找到具体的HandleExecution,HandleExecution会从url中查找出
适配器,并将其返回给DispatcherSevlet,DispatcherSerlet将设配器交给HandlerAdapter适配对应的
Controller,在Controller中调用业务层,在ModleAndView中添加数据,并将其返回给
HandlerAdapter,HandlerAdapter就将其返回给DispatcherServlet,DispatcherServlet将ModelAndView交给
视图解析器ViewResolver处理,将数据渲染到前端页面上,视图解析器ViewResolver 再将解析的视图返回给DispatcherSerlet,将其绘制在前端。

The picture shows a relatively complete flow chart of SpringMVC. The solid line represents the technology provided by the SpringMVC framework, which 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

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 is deployed on the web site on the server

test means the controller

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

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

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.

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

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

The Handler lets the specific Controller execute.

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

HandlerAdapter passes the view logical name or model to DispatcherServlet.

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

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

DispatcherServlet calls a specific view according to the view result parsed by the view parser.

The final view is presented to the user.

Guess you like

Origin blog.csdn.net/qq_44788518/article/details/112411587