zoukankan      html  css  js  c++  java
  • SpringMVC——DispatcherServlet

    SpringMVC——DispatcherServlet

    1、DispatcherServlet

    SpringMVC框架是围绕 DispatcherServlet 设计的,它处理所有的 HTTP 请求和响应。

    • 作用:

      DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,

      都将被它拦截,经过处理后再分发给不同的控制器!

    • 配置(必要的配置):

      <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_3_1.xsd"
          version="3.1">
      
          <!-- 配置前端控制器(DispatcherServlet) -->
          <servlet>
              <servlet-name>HelloWeb</servlet-name>
              <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
              <load-on-startup>1</load-on-startup><!-- 表示容器(Tomcat)在启动时立即加载这个 DispatcherServlet-->
          </servlet>
          <servlet-mapping>
              <servlet-name>HelloWeb</servlet-name>
              <url-pattern>*.do</url-pattern>
          </servlet-mapping>
      </web-app>
      
    • 加载Spring-web.xml配置文件的两个时机

      • 第一个:

        在 SpringMVC 项目启动时,会依据 web.xml 配置文件中所配置的监听器:ContextLoaderListener去加载对应位置下的 Spring 配置文件。

      <web-app ...>
          ...
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
                  classpath:spring/spring-dao.xml,
                  classpath:spring/spring-service.xml
              </param-value>
          </context-param>
          ...
      </web-app>
      
      • 第二个:

        无论该监听器有没有配置,那么 Spring MVC 都会继续进入第二个加载配置文件时机,根据 DispatcherServlet 的初始化配置(init-param)加载对应位置的 Spring 配置文件:

      <web-app ...>
          ...
          <servlet>
              <servlet-name>...</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:spring-web.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
          </servlet>
      
          <servlet-mapping>
              ...
          </servlet-mapping>
      </web-app>
      
      • 如果 init-param 没有配置,那么默认相当于是:
      <init-param>
          <param-name>contextConfigLocation</param-name>
            <param-value>WebContent/WEB-INF/[servlet-name]-servlet.xml</param-value>
      </init-param>
      

      注意:

      SpringMVC 首先加载的是 context-param配置的内容,而并不会去初始化 servlet。只有进行了网站的跳转,经过了 DispatcherServlet 的导航的时候,才会初始化 servlet,从而加载 init-param 中的内容。

    一般而言,context-param配置的 Spring 配置文件,习惯性叫 applicationContext,或 webApplicationContext,表示全局性的 Spring 配置。init-param 配置的 Spring 配置文件可以叫 spring-mvc,表示 spirng-webmvc (web 层)相关的 Spring 配置。

  • 相关阅读:
    HyperLeger Fabric开发(三)——HyperLeger Fabric架构
    WPF设计の画刷(Brush)
    WPF设计の画刷(Brush)
    WPF设计の画刷(Brush)
    Mina Basics 02-基础
    内置方法
    py-opp 类(class)
    Python 学习目录
    py-函数进阶
    py-函数基础
  • 原文地址:https://www.cnblogs.com/whitespaces/p/12450558.html
Copyright © 2011-2022 走看看