1.命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. </web-app>

2.web.xml流程

  1. 解析springmvc.xml文件
  2. 设置字符编码格式
  3. 解析applicationContext.xml

    3.解析springmvc

    前端控制器:DispatcherServlet

    <servlet>
         <servlet-name>mvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:springmvc.xml</param-value>
         </init-param>
    </servlet>
     <servlet-mapping>
         <servlet-name>mvc</servlet-name>
         <!-- /表示除了jsp以外的都可以访问 -->
         <url-pattern>/</url-pattern>
     </servlet-mapping>
    

    4.设置字符编码格式

    CharacterEncodingFilter是spring内置过滤器的一种,用来指定请求或者响应的编码格式服务器启动的时候就会创建Filter,将init-param中的参数加载,注入到CharacterEncodingFilter 类中,浏览器每次发送请求都会经过这个过滤器,然后调用doFilterInternal

     <filter>
         <filter-name>enc</filter-name>
         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
         </init-param>
     </filter>
    
     <filter-mapping>
         <filter-name>enc</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
    

    5.扫描applicationContext.xml文件

    ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
    
         <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:applicationContext.xml</param-value>
         </context-param>