1. Review the MVC

1.1 What is MVC?

  • MVC is a shorthand for Model, View and Controller. It is a software design specification
  • Is a way to organize code by separating business logic, data, and presentation
  • The main function of MVC is to reduce the bidirectional coupling between view and business logic
  • MVC is not a design pattern. MVC is an architectural pattern

1.2 What does the MVC framework do

  1. A method that maps a URL to a Java class or Java class
  2. Encapsulate user-submitted data
  3. Process the request -> invoke related business processing -> encapsulate the response data
  4. Render the response data (.jsp/.html) and other presentation layer data

1.3 Common MVC Framework

  • Server-side MVC frameworks include Struts, SpringMVC, ASP.NET, Zend Framework and JSF
  • Front-end MVC frameworks include Vue, AngularJS, React, and Backbone
  • Other patterns have evolved from MVC: MVP, MVVM, and so on

2. What is SpringMVC

2.1 for SpringMVC overview

  • SpringMVC is a part of the SpringFramework, which is a lightweight Web framework based on Java to implement MVC

2.2 Characteristics of SpringMVC

  • Lightweight and easy to learn
  • University, based on request response MVC framework
  • Good compatibility with Spring, seamless stitching
  • Convention over Configuration
  • Powerful functions: RESTful, data validation, formatting, themes and so on
  • Concise and flexible

Spring’s Web framework is designed around DispatcherServlet

The purpose of the DispatcherServlet is to distribute requests to different processors. Beginning with Spring2.5, users of Java5 and above can adopt annotation-based development

3. SpringMVC

3.1 Central Controller

Spring’s Web framework is designed around DispatcherServlet. The purpose of the DispatcherServlet is to distribute requests to different processors. As of Spring2.5, annotations based Controller declarations are available for Java5 and older

The Spring MVC framework, like many other MVC frameworks, is request-driven and dispatches requests and other functionality around a central Servlet, which is an actual Servlet (inherits the HttpServlet class).

The principle of SpringMVC is shown below:

  • When requested by the front controller intercept to the request, according to the parameters of the generated proxy requests, find request corresponding to the actual controller, the controller processes the request, to create the data model, database access, the model response is returned to the center controller, the controller using the model and view rendering results, return the results to the center controller, and then return the results to the requester

3.2 Implementation principle of SpringMVC

In the figure, the solid line represents the technology provided by the MVC framework (which does not require developer implementation), and the dotted line represents the technology required developer implementation

Analysis and execution process:

  1. The DispatcherServlet represents the front controller and is the control center for the entire SpringMVC. The user sends a request, and the DispatcherServlet receives the request and intercepts it
  2. HandlerMapping maps processors. The DispatcherServlet invokes HandlerMapping, which looks up the Handler based on the requested URL
  3. HandlerExecution represents a specific Handler whose primary purpose is to find a controller based on a URL
  4. HandlerExecution passes parsed information to the DispatcherServlet
  5. HandlerAdapter represents a processor adapter that executes handlers according to specific rules
  6. Handler lets specific controllers execute
  7. The Controller returns the specific execution information (ModelAndView) to the HandlerAdapter
  8. The HandlerAdapter will attempt to pass the logical name or model to the DispatcherServlet
  9. The DispatcherServlet Utility View parser (ViewResolver) resolves the logical view name passed by the HandlerAdapter
  10. The view parser passes the parsed logical view name to the DispatcherServlet
  11. The DispatcherServlet invokes specific views based on the view results parsed by the view parser
  12. The final view is presented to the user