zoukankan      html  css  js  c++  java
  • No WebApplicationContext found: no ContextLoaderListener registered

    修改前运行报错:No WebApplicationContext found: no ContextLoaderListener registered? 

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <filter>
          <filter-name>CharacterEncodingFilter</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>
    <!--       <init-param> -->
    <!--           <param-name>forceEncoding</param-name> -->
    <!--           <param-value>true</param-value> -->
    <!--       </init-param> -->
      </filter>
      <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    <!--   配置shiro 过滤器 -->
      <filter>
          <filter-name>shiroFilter</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
          <init-param>
              <param-name>targetFilterLifecycle</param-name>
              <param-value>true</param-value>
          </init-param>
          <init-param>
              <param-name>targetBeanName</param-name>
              <param-value>shiroFilter</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>shiroFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    <!--   配置springMVC核心控制器 -->
      <servlet>
          <servlet-name>DispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring_config.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>DispatcherServlet</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>

    <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 配置错误页面 --> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>503</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>400</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> </web-app>

    修改后:

    <!DOCTYPE web-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>web</display-name>  
      <filter>
          <filter-name>CharacterEncodingFilter</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>
    <!--       <init-param> -->
    <!--           <param-name>forceEncoding</param-name> -->
    <!--           <param-value>true</param-value> -->
    <!--       </init-param> -->
      </filter>
      <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    <!--   配置shiro 过滤器 -->
      <filter>
          <filter-name>shiroFilter</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
          <init-param>
              <param-name>targetFilterLifecycle</param-name>
              <param-value>true</param-value>
          </init-param>
          <init-param>
              <param-name>targetBeanName</param-name>
              <param-value>shiroFilter</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>shiroFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring_config.xml</param-value>
        </context-param>
      
    <!--   配置springMVC核心控制器 -->
      <servlet>
          <servlet-name>DispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring_mvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>DispatcherServlet</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <welcome-file-list>
          <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
           
      <!-- 配置错误页面 -->
      <error-page>
          <error-code>404</error-code>
          <location>/WEB-INF/jsp/common/error.jsp</location>
      </error-page>
      <error-page>
          <error-code>500</error-code>
          <location>/WEB-INF/jsp/common/error.jsp</location>
      </error-page>
      <error-page>
          <error-code>503</error-code>
          <location>/WEB-INF/jsp/common/error.jsp</location>
      </error-page>
      <error-page>
          <error-code>400</error-code>
          <location>/WEB-INF/jsp/common/error.jsp</location>
      </error-page>
    </web-app>

    原因分析:

    <filter>  
            <filter-name>shiroFilter</filter-name>  
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
            <init-param>  
                <param-name>targetFilterLifecycle</param-name>  
                <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>shiroFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
            <dispatcher>REQUEST</dispatcher>  
            <dispatcher>FORWARD</dispatcher>  
        </filter-mapping> 

      项目加载的shiro过滤器的功能需要依赖ContextLoaderListener所加载的apring_config.xml文件生成的applicationContext即Spring的上下文,而之前DispatcherServlet所加载的spring_mvc.xml文件生成的applicationContext为Spring mvc的上下文,shiro过滤器无法加载,所以得依靠监听器加载public的applicationContext。

    三个context之间的的关系:

    1. 对于一个web应用,web容器加载一个全局context(即servletContext)供其使用,为后面的spring IoC容器提供依赖。
    2. 在web.xml中提供的ContextLoaderListener监听器,web容器启动时会触发容器初始化事件,ContextLoaderListener会监听这个事件,初始化一个根上下文即WebApplicationContext,实现类为XmlWebApplicationContext。这个就是Spring的IoC容器,spring将其以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为key存储在servletContext中
    3. DispatcherServlet在初始化会建立自己的IoC容器用以持有Spring mvc相关的bean,它在建立的时候会通过WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先从servletContext中获取根上下文作为自己上下文的父上下文,它的实现类也是XmlWebApplicationContext,在建立之后会以和自己servlet-name便签有关的名称存储在servletContext中,这样每个servlet就拥有自己独立的bean及根上下文共享的bean

      但是这样会导致重复加载配置文件,DispatcherServlet、ContextLoaderListener会分别加载一次,解决方法是把配置文件拆分成2个,一个Spring mvc的配置文件,一个Spring的配置文件,DispatcherServlet加载Spring mvc的配置文件,ContextLoaderListener加载Spring的配置文件 。

    Spring mvc在使用上下文的时候会使用DispatcherServlet加载的applicationContext,也会使用的contextLoaderListener加载的applicationContext。

  • 相关阅读:
    学习笔记 四边形不等式的模型
    题解 AT2230【Water Distribution】
    题解 CF848C 【Goodbye Souvenir】
    第二届全国大学生算法设计与编程挑战赛(春季赛)正式赛题解&比赛回顾
    积性函数相关学习笔记
    【MySQL】数据库备份
    【SpringMVC】SSM的maven依赖
    【SpringMVC】Jackson解决乱码问题配置
    【SpringMVC】DispatcherServlet与中文过滤器配置
    【Spring】学习笔记008--AOP实现方式
  • 原文地址:https://www.cnblogs.com/kuoAT/p/8423518.html
Copyright © 2011-2022 走看看