Get to know spring MVC


What is MVC

First, let’s find out what MVC is,

  • MVC refers to model, view and controller. It is a software design specification.
  • It is a method to organize code by separating business logic, data and display.
  • The main function of MVC is to reduce the two-way coupling between view and business logic.
  • MVC is not a design pattern, MVC is an architecture pattern. Of course, there are differences between different MVCs.

Model:The data model provides the data to be displayed, so it contains data and behavior. It can be considered as a domain model or JavaBean component (including data and behavior), but now it is generally separated: value object (data Dao) and service layer (behavior service). That is, the model provides functions such as model data query and model data status update, including data and business.

View:Be responsible for displaying the model, which is generally the user interface we see and what customers want to see.

Controller:Receive the user’s request and delegate it to the model for processing (state change). After processing, return the returned model data to the view, which is responsible for displaying it. In other words, the controller does the work of a dispatcher.

The most typical MVC is the pattern of JSP + servlet + JavaBean.

What is spring MVC

Spring MVC is a part of the spring framework and a lightweight web framework based on Java to implement MVC.

Features of spring MVC:

  1. Lightweight, easy to learn

  2. Efficient, request response based MVC framework

  3. Good compatibility with spring and seamless combination

  4. Convention over configuration

  5. Powerful functions: restful, data validation, formatting, localization, theme, etc

  6. Concise and flexible

Spring’s web framework revolves aroundDispatcherServlet[scheduling servlet] design.

The dispatcher servlet is used to distribute requests to different processors. Starting from spring 2.5, users using Java 5 or above can develop based on annotations, which is very concise;

Let’s understand the specific working principle of spring MVC through a figure on the Internet.

Get to know spring MVC

The general process is as follows

  • 1. The user sends a request URL, which will pass through the front-end controller, dispatcherservlet. Dispatcher servlet calls handlermapping. Dispatcherservlet will find the mapper according to the request. Handlermapping will find the handler according to the request URL.
  • 2. Handlerexecution refers to a specific handler, which is mainly used to find the controller according to the URL. Here is hello.
  • 3. Find the adapter according to the mapper, handleradapter,
  • 4. Adapt to the control layer code controller we wrote, and then the controller calls the service layer, and the service layer calls the Dao layer to perform crud and other operations on the database, and then return the modelandview, that is, the connection between data and jump.
  • 5. Find the specific view parser according to modelandwiev, and the view parser returns to the front end.

This work adoptsCC agreement, reprint must indicate the author and the link to this article