project Structure删除原有的web.xml,创建其他名字的web.xml(之后改回这个名字)

    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. <!--添加中文编码过滤器-->
    7. <filter>
    8. <filter-name>filter1</filter-name>
    9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    10. <!--
    11. private String encoding;
    12. private boolean forceRequestEncoding = false;
    13. private boolean forceResponseEncoding = false;
    14. -->
    15. <init-param>
    16. <param-name>encoding</param-name>
    17. <param-value>UTF-8</param-value>
    18. </init-param>
    19. <init-param>
    20. <param-name>forceRequestEncoding</param-name>
    21. <param-value>true</param-value>
    22. </init-param>
    23. <init-param>
    24. <param-name>forceResponseEncoding</param-name>
    25. <param-value>true</param-value>
    26. </init-param>
    27. </filter>
    28. <filter-mapping>
    29. <filter-name>filter1</filter-name>
    30. <url-pattern>/*</url-pattern>
    31. </filter-mapping>
    32. <!--注册springmvc框架-->
    33. <servlet>
    34. <servlet-name>springmvc</servlet-name>
    35. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    36. <init-param>
    37. <param-name>contextConfigLocation</param-name>
    38. <param-value>classpath:springmvc.xml</param-value>
    39. </init-param>
    40. </servlet>
    41. <servlet-mapping>
    42. <servlet-name>springmvc</servlet-name>
    43. <url-pattern>/</url-pattern>
    44. </servlet-mapping>
    45. <!--注册spring框架,目的就是启动spring容器-->
    46. <!--用监听器去使用-->
    47. <listener>
    48. <listener-class>ContextLoaderListener</listener-class>
    49. </listener>
    50. </web-app>

    **ContextLoaderListener**我们要找源码

    **Maven:org.springframework:spring-web:版本号**->**org.springframework.web.context.ContextLoaderListener**

    public class ContextLoaderListener extends ContextLoader implements ServletContextListener/*整个项目相关的监听器*/ {
        ...
        //初始化
        @Override
        public void contextInitialized(ServletContextEvent event) {
            initWebApplicationContext(event.getServletContext());
        }
    
        //销毁
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            closeWebApplicationContext(event.getServletContext());
            ContextCleanupListener.cleanupAttributes(event.getServletContext());
        }
        ...
    }
    

    我们只不过是把spring的容器放在了这个方法中,让它去启动

    <?xml version="1.0" encoding="UTF-8"?>
    <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_4_0.xsd"
             version="4.0">
        <!--添加中文编码过滤器-->
        <filter>
            <filter-name>filter1</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <!--
            private String encoding;
           private boolean forceRequestEncoding = false;
           private boolean forceResponseEncoding = false;
           -->
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceRequestEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>forceResponseEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>filter1</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!--注册springmvc框架-->
        <servlet>
            <servlet-name>springmvc</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>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!--注册spring框架,目的就是启动spring容器-->
        <!--用监听器去使用-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <!--通配符表示,所有以'applicationContext_'开头的配置文件都注册-->
            <param-value>classpath:applicationContext_*.xml</param-value>
        </context-param>
    </web-app>
    

    这就是我们web.xml文件的注册