Spring MVC learning notes 01 - spring MVC implementation principle

1. What is spring MVC

Spring MVC is a part of the Spring Framework and a lightweight Web framework based on Java to implement MVC. Spring MVC is designed around dispatcher servlet.


The function of dispatcher Servlet is to distribute the requests sent by users to different servlets. The dispatcher Servlet is actually a Servlet (it inherits from the HttpServlet base class). As shown in the figure below:

3. Implementation principle of spring MVC

The execution principle of spring MVC is shown in the following figure:

As shown in the figure above, the process executed is:
1. After the user sends a request to the server (such as tomcat), it will be handed over to the front-end controller dispatcher servlet for processing

2. After the dispatcher servlet receives the request, it calls HandlerMapping. HandlerMapping finds the specific processor according to the requested url, generates the processor execution chain, and returns the handlerexecutionchain (including the processor object and the processor interceptor) to the dispatcher servlet. Here, it can be simply understood as obtaining the specific controller bean name according to the requested url.

3.DispatcherServlet gives the returned results of HandlerMapping to the HandlerAdapter, which will execute the specific controller according to these returned results

4. The controller usually needs to call the methods of the service layer, and then the service layer calls the dao layer. The dao layer calls jdbc or Mybatis to operate the database and return the results to the service layer. Finally, the controller obtains the data returned by the service layer and returns ModelAndView to the HandlerAdapter.

5. The handleradapter then passes the ModelAndView to the dispatcher servlet

6. The dispatcherservlet then passes the ModelAndView to the viewresolver view parser

7. After the viewsolver parses the ModelAndView, it can obtain the specific View and return the View to the dispatcher servlet

8. After receiving the View, the dispatcher servlet can render the View (that is, fill the model data model into the View)

9. Finally, DispatcherServlet returns the rendered View to the user

Note: during the running and implementation of spring MVC program, some configurations need to be completed accordingly.

1. It needs to be on the web When configuring any request url to access the server in the XML file, it needs to be processed by the dispatcher servlet and associated with a spring MVC configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

    <!--1.register DispatcherServlet-->
        <!--Associate a springmvc Configuration file for:[servlet-name]-servlet.xml-->
        <!--Startup level-1-->

    <!--/ Match all requests; (excluding.jsp)-->
    <!--/* Match all requests; (including.jsp)-->


2. HandlerMapping, HandlerAdapter, ViewResolver and specific controllers are registered in the configuration file of spring MVC. These bean s will be instantiated when the request is received by DispatcherServlet for the first time.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <!--Process mapper-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--Processing adapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!--view resolver :For parsing DispatcherServlet Passed on ModelAndView-->
        1.obtain ModelAndView Data
        2.analysis ModelAndView View name for
        3.Splice the view name and find the corresponding view
        4.Render data to view
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>


    <bean id="/hello" class="controller.HelloController"/>


Some contents are reproduced to: Mad God says spring mvc01: what is spring MVC

Keywords: Spring MVC

Added by javier on Sat, 01 Jan 2022 06:52:05 +0200