Introduction of the MVC

  • MVC is a software design specification, short for Model, View and Controller.

  • MVC is a common architectural pattern designed for decoupling!

  • Model: The data Model provides the data to be presented on the page, also known as the business logic layer. The model layer is a broad overview. The model layer includes the Service layer and the Dao layer.

  • View: responsible for the data model + View frame display, that is, we see the web page!

  • Controller (Controller) : Receives user requests and delegates them to the model for processing. After processing, the returned model data is returned to the view for display.

  • Before MVC architecture was proposed, the development of Web pages was only model and view. So there’s no Controller, so why is MVC succeeding in replacing the traditional two-tier architecture

Here is a personal collation of some information, the need of friends can be directly click to receive.

Java Basics

22 Java Architect Core books

Learning routes and materials from 0 to 1Java

1000+ questions from 2021

Advantages:

The architecture is simple and easy to implement.

Disadvantages:

The view layer has more responsibilities than one; Not only do you have to encapsulate the data, but you also have to write the logical code to call the model layer which means that the view layer acts as both view + control; The view layer deals directly with the model layer and the pages are messy and not maintainable

The MVC architecture is proposed to separate the view from the model layer. But through the control layer to bridge the interaction between the two;

The view layer only needs to focus on data encapsulation and presentation

The model layer focuses on business logic

The control layer handles user-submitted requests and coordinates the view and model layers

SpringMVC executes the process

The core of SpringMVC framework revolves around the front-end controller of DispatcherServlet, which is used to coordinate all servlets to parse the user’s request, find the corresponding Servlet for processing, and finally give the response! You can make the DispatcherServlet function like a CPU processor, the human brain…

  • The user initiates the request through the view page or url address path, and the front-end control DispatcherServlet receives the request from the user and starts to operate!
  • The DispatcherServlet calls HandlerMapping to find the Handler that is ultimately used for execution
  • HandlerExecution includes the specific executor Handler and HandlerInterceptor processor interceptor. It is returned to the DispatcherServlet of the front-end controller.
  • The HandlerExecution object obtained is matched with the corresponding processor adapter HandlerAdapter and parsed.
  • The processor adapter HandlerAdapter finally successfully matches the Servlet class of the Controller layer written by the programmer.
  • The Controller layer has distinct responsibilities. It calls the model layer to access the database and obtain the data and views that need to be responded to the user at last.
  • The Controller layer encapsulates data and views in ModelAndView objects and returns them to the processor adapter HandlerAdapter.
  • The processor adapter HandlerAdapter receives the results returned by the Controller for processing and then passes them to the front-end Controller DispatcherServlet.
  • The front-end controller DispatcherServlet calls the ViewResolver; The ViewResolver parses the data in the ModelAndView, parses the view name of the response, finds the corresponding view, and wraps the data into the view!
  • The ViewResolver returns the view name to the front-end controller DispatcherServlet, and the front-end controller DispatcherServlet calls the response view to the user!

The first SpringMVC application

The JSP page

<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>${message}</h1> </body> </html>Copy the code

Write the corresponding Servlet (Controller)

public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { // 1. ModelAndView mv = new ModelAndView(); Encapsulate data object mv.addobject ("message","Hello, SpringMVC!") ); // 3. Encapsulate the view to jump to and place it in ModelAndView mv.setViewName("hellomvc"); return mv; }}Copy the code

Configure the SpringMVC core file

In the core configuration file, configure mapper, adapter, parser; Finally, the requested path and the corresponding Servlet class are handed over to the IOC container for hosting.

<? The XML version = "1.0" encoding = "UTF8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <! - 1. Configuration processor mapper -- > < bean class = "org. Springframework. Web. Servlet. Handler. BeanNameUrlHandlerMapping" / > <! - 2. Configuration processor adapter - > < bean class = "org. Springframework. Web. Servlet. MVC. SimpleControllerHandlerAdapter" / > <! - 3. The parser configuration view - > < bean id = "InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <! Prefix -- - > < property name = "prefix" value = "/ WEB - INF/page/" / > <! -- suffix - > < property name = "suffix" value = "JSP" / > < / bean > <! 4 - the servlet to the IOC container management - > < bean id = "/ hellomvc" class = "com. Controller. HelloController" / > < / beans >Copy the code

Configure mapping path processing

Because all sevlets do not follow their own mapping path, but are dispatched by the front-end controller DispatcherServlet, you only need to configure DispatcherServlet in the project web.xml. Then hand over the SpringMVC core configuration file to the front-end controller DispatcherServlet!

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" Version = "4.0" > <! <servlet> < servletname > Springmvc </ servletname > <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! > <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC-Servlet.xml</param-value> </init-param> <! Startup level 1 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> Springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

The test results

Analysis and Summary

In web. XML, there is no need to configure the mapping path of individual servlets. Instead, DispatcherServlet is configured directly. This is because the front controller does its own lookup against HandlerMapping.

The Servlet does not need to inherit from the HttpServlet class because DispatcherServlet inherits from HttpServlet. Now the Servlet that implements the Controller interface will be found through the HandlerAdapter!

The last

Give it a thumbs up if you find it helpful.