SpringMVC-DispatcherServlet工作流程及web.xml配置

工作流程:
https://www.cnblogs.com/yw0219/p/6084128.html
Web中,无非是请求和响应;
在SpringMVC中,请求的第一站是DispatcherServlet,充当前端控制器角色;
DispatcherServlet会查询一个或多个处理器映射(handler mapping)并根据请求所携带的URL信息进行决策,将请求发送给哪个SpringMVC控制器(controller);
控制器做两件事:一是将数据打包,二是定义逻辑视图名,然后返回给DispatcherServlet;
DispatcherServlet通过视图解析器(view resolver)来将逻辑视图名匹配为一个特定的视图实现,它可能是也可能不是JSP;
交付数据模型,以视图形式响应给客户,整个请求流程完成。

web.xml

<welcome-file-list>[欢迎页面,可定义多个,会依次查找可用视图]
<listener>
<listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j监听器在前,目前已废除log4j监听器,原因还在努力追问
<context-param>指定上下文配置文件路径,基本配置包含log4j和Spring配置文件
<param-name>指定上下文名称,一般为:名称+ConfigLocation后缀,如:contextConfigLocation,不可随意定义,否则指定的配置文件无法加载成功,实际上它是org.springframework.web.servlet.FrameworkServlet中的一个成员变量,而FrameworkServlet是DispatcherServlet的父类,log4jConfigLocation目前不得而知
<param-value>指定上下文路径,如:classpath:applicationContext.xml
<servlet>
<servlet-name>Servlet名称,可以自定义,但是需要遵守规则:比如指定为Spring,那么最好在classpath路径中配置Spring-servlet.xml,否则需要在子元素<init-param>特别指出
<servlet-class>因为要配置MVC,所以指定为:org.springframework.web.servlet.DispatcherServlet
<init-param>[定义容器启动时初始化的配置文件,作用主要是指定自定义配置文件的路径,貌似可以指定多个]
<param-name>[contextConfigLocation,不可更改,原因见3.1]
<param-value>[可以自定义,如:classpath:spring-servlet.xml,如果不定义,那么默认为:classpath:${servlet-name}-servlet.xml,见4.1]
<load-on-startup>[定义为1,表示启动等级,参考文章]
<servlet-mapping>
<servlet-name>与4.1保持一致
<url-pattern>一般定义为“/”,表示所有请求都通过DispatcherServlet来处理
<filter>[以字符集为例]
<filter-name>[自行指定]
<filter-class>[org.springframework.web.filter.CharacterEncodingFilter]
<init-param>
<param-name>[encoding,不可更改,它是CharacterEncodingFilter中定义的一个成员变量]
<param-value>[UTF-8]
<filter-mapping>
<filter-name>[与6.1保持一致]
<url-pattern>[/*表示所有请求都经过此过滤器过滤]

<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>display-name>    
  <welcome-file-list>
    <welcome-file>/WEB-INF/views/home.jspwelcome-file>
  welcome-file-list>

  <context-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
    <context-param>   
   <param-name>log4jConfigLocationparam-name>   
   <param-value>classpath:log4j.propertiesparam-value>   
context-param>
  
<listener>   
   <listener-class>   
        org.springframework.web.util.Log4jConfigListener   
   listener-class>   
listener> 
<listener>   
     <listener-class>   
          org.springframework.web.context.ContextLoaderListener   
     listener-class>   
listener>
<servlet>
      <servlet-name>Springservlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
      
      <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:Spring-servlet.xmlparam-value>
      init-param>
      <load-on-startup>1load-on-startup>
      servlet>
      <servlet-mapping>
          <servlet-name>Springservlet-name>
          
          <url-pattern>/url-pattern>
      servlet-mapping>
    
        <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
      filter>

      <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>  
      filter-mapping>
web-app>

你可能感兴趣的:(SpringMVC-DispatcherServlet工作流程及web.xml配置)