An overview of the

The DispatcherServlet is the SpringMVC control center, the user makes a request, it receives the request and intercepts the request to register the controller in web.xml

<! DOCTYPEweb-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <! - 1. Registered DispatcherServlet -- -- >
  <servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <! -- Associated with a springMVC configuration file: [servlet-name] -- servlet.xml-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <! -- Boot level -- 1-->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <! --/ Match all requests; (not including.jsp) -->
  <! --/* Match all requests; (including.jsp) -->
  <servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Copy the code

We requested url is: http://localhost:8080/springmvc/hello 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.

We use HandlerMapping to find handlers based on the requested URL, and HanlderExecution to find the controller based on the URL. This example is Hello


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <! -- Automatically scan packets to validate annotations under specified packets, managed by IOC containers -->
    <context:component-scan base-package="org.example.controller"/>
    <! Let Spring MVC not handle static resources -->
    <mvc:default-servlet-handler />
    <! - support the MVC annotation driven @ RequestMapping commonly used in spring annotations to complete the mapping relationship To make the @ RequestMapping annotations to take effect Must be registered DefaultAnnotationHandlerMapping with the context And a AnnotationMethodHandlerAdapter instance this two instances in class level and treatment level. The annotation-driven configuration helps us to automate the injection of the above two instances. -->
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <! -- View parser :DispatcherServlet to its ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <! -- prefix -- -- >
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <! - the suffix - >
        <property name="suffix" value=".jsp"/>
    </bean>

    <! --Handler-->
    <bean id="/hello" class="org.example.controller.HelloController"/>

</beans>
Copy the code

This line in the configuration file

 <bean id="/hello" class="org.example.controller.HelloController"/>
Copy the code

HandlerExecution passes parsed information to the DispatcherServlet, such as parsed controller mappings.

HandlerAdapter represents a processor adapter that executes handlers according to specific rules.

Handler lets specific controllers execute.

The Controller returns specific execution information to the HandlerAdapter, such as ModelAndView.

The HandlerAdapter passes the view logical name or model to the DispatcherServlet.

The DispatcherServlet calls the ViewResolver to resolve the logical view name passed by the HandlerAdapter.

The view parser passes the parsed logical view name to the DispatcherServlet.

The DispatcherServlet invokes specific views based on the view results parsed by the view parser.

The final view is presented to the user.