SpringMVC and how it works

This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Review the back end.

1. Understand SpringMVC

MVC(Model View Controller) is a framework pattern of software design. MVC is short for Model, View and Controller. It organizes code by separating business logic, data and display. The main effect is to reduce the two-way coupling between the view and the business logic, thus achieving the separation of the front and back end code.

Three core components:

Model: Data Model, which provides the data to be presented and therefore contains the data and behavior, can be thought of as a domain Model or JavaBean component (containing the data and behavior), but is now generally separated: Value Object (Data Dao) and behavior Service layer (behavior Service). That is, the model provides model data query and model data status update functions, including data and business.

View: Is responsible for presenting the model, which is generally the user interface we see, what the customer wants to see. In Web development, that’s JSP pages.

Controller (Controller) : Receives user requests, delegates them to the model for processing (state changes), and returns the returned model data to the view, which is responsible for displaying it. So the controller is doing the job of a dispatcher.

Second, the advantages of SpringMVC

  • SpringMVC is flexible, non-invasive, and configurable.
  • SpringMVC has a distinct division of work, including controllers, validators, command objects, model objects, handler map view parsers, and so on, with each function implemented by a dedicated object.
  • Powerful and straightforward configuration: Both framework and application classes can be configured as Javabeans, supporting references across multiple contexts.
  • Reusable business code: You can use existing business objects as command or form objects without having to extend the base classes of a particular framework.
  • Spring Tag Library simple and powerful JSP Tag Library: support includes many features such as data binding and themes. It provides maximum flexibility in marking.
  • Customizable Handler Mapping and View Resolution: Spring provides everything from the simplest URL mapping to complex, specialized customization strategies. Spring is more flexible than some Web MVC frameworks that force developers to use a single specific technology.

The principle of SpringMVC

Let’s analyze the process

A complete request in SpringMVC works as shown below:

1, the user sends a request to the server, the request is Spring front controller servlets DispatcherServlet capture For example: http://localhost:8080/SpringMVC/hello

The above URL is divided into three parts:

http://localhost:8080 Server domain name

SpringMVC is deployed on a web site on a server

Hello means controller

Through analysis, the above URL is represented as: request the Hello controller of the SpringMVC site at server localhost:8080.

2. DispatcherServlet parses the request URL and obtains the request resource Identifier (URI). Then, based on the URI, HandlerMapping is called to retrieve all relevant objects (including Handler objects and interceptors corresponding to Handler objects) configured by this Handler, which are returned as HandlerExecutionChain objects

3. The DispatcherServlet selects an appropriate HandlerAdapter according to the obtained Handler.

4. Extract the model data from the Request, populate the Handler entry parameter, and start Handler (Controller). Depending on your configuration, Spring will do some extra work for you during the entry process of populating the Handler, such as converting the request message (such as Json, XML, etc.) into an object and converting the object into the specified response information

5. Handler returns a ModelAndView object to the DispatcherServlet after execution

6. According to the returned ModelAndView, select a suitable ViewResolver (must be a ViewResolver registered in the Spring container) and return it to the DispatcherServlet

ViewResolver combines Model and View to render the View

8. Return the render result to the client

This is the complete request execution step, we just do the view and controller writing, spring does the rest for us.

Study notes shared today. Come on!