Spring MVC- Principle

1.1 Basic Features of Spring MVC

1.1.1 Basic concepts

  • Spring MVC is a lightweight Web MVC framework of the Spring system.
  • The core Controller of Spring MVC is used to process requests and generate responses.
  • Spring MVC runs based on the Spring IOC container, and all objects are managed by the IOC.

1.1.2 Position in the three-tier architecture

1.1.3 Advantages of Springmvc

Serial number advantage description
1 Clear role division Front controller (DispatcherServlet), processor mapper (HandlerMapping), processor adapter (HandlerAdapter), view resolver (ViewResolver), back-end controller (Controller)
2 Seamless integration with Spring framework This is not available in other web frameworks
3 Good scalability Can be easily extended, although hardly needed
4 Convenient unit testing The Mock object provided by Spring can be used to perform Web layer unit testing very simply
5. Powerful RESTful, data validation, formatting, localization, themes, etc.
6 jsp tag library Powerful JSP tag library, making JSP writing easier

1.1.4 Comparison with Struts2

Common points :

  • They are all presentation layer frameworks and are written based on the MVC model.
  • Their bottom layer is inseparable from the original ServletAPI.
  • Their mechanism for processing requests is a core controller.

Difference :

  • The entrance of Spring MVC is Servlet, and Struts2 is Filter.
  • Spring MVC is based on method design, and Struts2 is based on class, Struts2 will create an action class every time it is executed. So Spring MVC will be slightly faster than Struts2.
  • Spring MVC is more concise to use, and it also supports JSR303, making it more convenient to process ajax requests.
  • Struts2's OGNL expression makes the page development efficiency higher than Spring MVC, but the execution efficiency is not better than JSTL, especially the struts2 form tag, which is not as efficient as html.

1.2 Principle of execution

1.2.1 Front 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) .

1.2.2 Principles of SpringMVC

When the request is initiated, the front controller intercepts the request, generates a proxy request according to the request parameters, finds the actual controller corresponding to the request, the controller processes the request, creates a data model, accesses the database, and responds the model to the central controller to control The processor 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.

1.2.3 SpringMVC execution principle

The solid line indicates the technology provided by the SpringMVC framework and does not need to be implemented by the developer, and the dotted line indicates that it needs to be implemented by the developer.

1.2.4 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.
Suppose the requested url is: http://localhost:8080/SpringMVC/success

如上url拆分成三部分:http://localhost:8080服务器域名。
SpringMVC部署在服务器上的web站点。success表示控制器.
通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。

2. HandlerMapping is the processor mapping.

DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。

3. HandlerExecutionChain represents a specific Handler (processor)

其主要作用是根据url查找控制器,如上url被查找控制器为:success。

4. HandlerExecutionChain 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. The Handler lets the 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 a specific view according to the view result parsed by the view parser.

12. The final view is presented to the user.

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/115190332