1. Introduction to MVC
-
MVC is a software design specification, which is the abbreviation of Model, View and Controller
-
MVC is a common architecture pattern. Its purpose is to decouple!
-
Model: the data model provides the data to be displayed on the page, also known as the business logic layer. The model layer is a broad overview. The model layer includes Service layer and Dao layer.
-
View: responsible for displaying the data model + view framework, that is, the web page we see!
-
Controller: receive the user's request and delegate it to the model for processing. After processing, the returned model data is returned to the view, which is responsible for displaying it.
Before MVC architecture was put forward, the development of Web page had only two layers: model and view; In other words, there is no Controller control layer. Let's see why MVC successfully replaces the traditional two-tier architecture
Advantages: simple architecture and easy implementation.
Disadvantages: the responsibility of view layer is not single; We not only need to encapsulate the data, but also need to write logic code to call the model layer, that is, the view layer here serves as the two responsibilities of view + control; The view layer directly deals with the model layer, and the page is chaotic and not conducive to maintenance
MVC architecture is proposed to separate the view from the model layer, and they do not deal directly with each other; But through the control layer to form a bridge 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 is responsible for handling the requests submitted by users and coordinating the view and model layers
2. Spring MVC execution process
The core of spring MVC framework revolves around the DispatcherServlet front-end controller. It is used to coordinate all servlets to parse the user's request, find the corresponding Servlet for processing, and finally give a response! DispatcherServlet function can be similar to CPU processor, human brain
1. The user initiates a request through the view page or url address path, and the front end controls the dispatcher servlet to receive the user's request and start operation!2. DispatcherServlet calls HandlerMapping to find the final Handler for execution
3. HandlerExecution includes specific executor Handler, HandlerInterceptor processor interceptor. Return it to the front-end controller DispatcherServlet.
4. Match the obtained HandlerExecution object with the corresponding processor adapter HandlerAdapter and parse it.
5. The processor adapter HandlerAdapter finally successfully matches the Servlet class of the Controller layer written by the programmer.
6. The controller layer has clear responsibilities, calls the model layer to access the database, and obtains the data and views that need to be responded to the user at last.
7. The controller layer encapsulates the data and view in the ModelAndView object, and then returns it to the processor adapter HandlerAdapter.
8. The processor adapter HandlerAdapter receives the result returned by the Controller for processing, and then hands it over to the front-end Controller DispatcherServlet.
9. The front-end controller DispatcherServlet calls the view parser ViewResolver; ViewResolver parses the data in ModelAndView, parses the view name of the response, finds the corresponding view, and finally encapsulates the data into the view!
10. The view parser ViewResolver returns the name of the view to the front-end controller DispatcherServlet. Finally, the view of the response called by the front-end controller DispatcherServlet is displayed to the user!
3. The first spring MVC program
3.1. JSP page
Write a hellomvc.exe in the page folder under the WEB-INF directory JSP page
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>${message}</h1> </body> </html>
3.2. Write the corresponding Servlet (Controller)
Writing servlets in Java Web stage needs to inherit HttpServlet. Now you can directly implement the Controller interface
public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { // 1. Create model - View ModelAndView mv = new ModelAndView(); //Call business layer // 2. Encapsulate data objects mv.addObject("message","Hello, SpringMVC!"); // 3. Encapsulate the view to jump to and put it in ModelAndView mv.setViewName("hellomvc"); return mv; } }
3.3. Configure spring MVC core file
In the core configuration file, configure the mapper, adapter and parser; Finally, give the requested path and the corresponding Servlet class to the IOC container for hosting.
<?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. Configure processor mapper --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 2. Configure processor adapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 3. Configure view parser --> <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. take servlet hand IOC Container management --> <bean id="/hellomvc" class="com.controller.HelloController"/> </beans>
3.4. Configure mapping path processing
Since all sevlets do not follow their own mapping paths, but are uniformly scheduled by the front-end controller DispatcherServlet, they only need to be on the web of the project Just configure DispatcherServlet in XML. Then hand over the spring MVC core configuration file to the front-end controller dispatcher servlet!
<?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"> <!--1.register DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- relation Spring Core profile for --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC-Servlet.xml</param-value> </init-param> <!-- Start 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>
3.5 test results
4. Analysis and summary
-
web. There is no need to configure the mapping path of a separate Servlet in XML, but directly configure DispatcherServlet. This is because the front-end controller will find it by itself according to HandlerMapping.
-
Servlets do not need to inherit the HttpServlet class because DispatcherServlet inherits HttpServlet. The Servlet written now implements the Controller interface and will be found through the adapter HandlerAdapter!