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>
原因分析:
项目加载的shiro过滤器的功能需要依赖ContextLoaderListener所加载的apring_config.xml文件生成的applicationContext即Spring的上下文,而之前DispatcherServlet所加载的spring_mvc.xml文件生成的applicationContext为Spring mvc的上下文,shiro过滤器无法加载,所以得依靠监听器加载public的applicationContext。
三个context之间的的关系:
- 对于一个web应用,web容器加载一个全局context(即servletContext)供其使用,为后面的spring IoC容器提供依赖。
- 在web.xml中提供的ContextLoaderListener监听器,web容器启动时会触发容器初始化事件,ContextLoaderListener会监听这个事件,初始化一个根上下文即WebApplicationContext,实现类为XmlWebApplicationContext。这个就是Spring的IoC容器,spring将其以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为key存储在servletContext中
- 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。
No WebApplicationContext found: no ContextLoaderListener registered的更多相关文章
- No WebApplicationContext found: no ContextLoaderListener registered?报错解决
今天跑了下新搭的一个SSI框架. 报例如以下错误: 严重: Exception sending context initialized event to listener instance of cl ...
- 使用shiro 框架 报错No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
1.问题描述:ssm 框架中使用shiro 中出现问题 原来web.xml 文件如下: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, ...
- 【转】ContextLoaderListener 和 DispatcherServlet
转载地址: http://www.guoweiwei.com/archives/797 DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供S ...
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件 ...
- 获取WebApplicationContext的几种方式
加载WebApplicationContext的方式 WebApplicationContext是ApplicationContext的子接口,纵观Spring框架的几种容器,BeanFactory作 ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】
原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...
- 关于在filter中获取WebApplicationContext的实践
网上很多说法,诸如: <param-name>contextConfigLocation</param-name> <param-value> classpath: ...
- WebApplicationContext初始化的两种方式和获取的三种方式
原博客地址:http://blog.csdn.net/lmb55/article/details/50510547 接下来以ContextLoaderListener为例,分析它到底做了什么? app ...
随机推荐
- Bcompare 提示 “这个授权密钥已被吊销” 解决方法
Bcompare 提示 “这个授权密钥已被吊销” 解决方法 打开文件夹 %appdata%\Scooter Software 找到相应的版本,例如 Beyond Compare 3 删除里面的 BCS ...
- P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校").注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学 ...
- 关于strlen和sizeof的使用
在学习C语言中发现strlen和sizeof的关系不是很明确,今天来总结一下这两个的区别: sizeof 是运算符,用来计算字节数,在计算字符串数组大小时包含(\0) 在编译时计算大小,参数可以是数组 ...
- Git的撤销操作
https://blog.csdn.net/qq_36431213/article/details/78858848 Git 初接触 (三) Git的撤销操作 git reset HEAD -- gi ...
- python - django (母版与组件)
# 把多个页面通用的部分提取出来 放在一个母版中 # 其它的页面只需要继承 母版就可以 # 使用步骤:( 继承的语句要放在第一行 ) """ 1. 把公用的 HTML 部 ...
- Vue多语言支持
i18n插件实现多语言支持,本文以中英文为例记录一下配置过程. 1.配置 1.1安装:npm install vue-i18n --save 1.2创建中英文配置项文件 src/lang目录下创建以下 ...
- centos svn的安装和配置
1.安装svn yum -y install subversion 2.配置 mkdir /home/svn/admin/test mkdir /home/svn/svnrepos svnadmin ...
- Oracle的dual是什么东西啊??(答:伪表)
1. dual 确实是一张表.是一张只有一个字段,一行记录的表.2.习惯上,我们称之为'伪表'.因为他不存储主题数据.3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.如:se ...
- P/NP问题
目录 P NP NPC NPH 写在开头 1.多项式 如公式:y = axn-bxn-1+c.Ο(log2n).Ο(n). Ο(nlog2n).Ο(n2)和Ο(n3)称为多项式时间.Ο(2n)和Ο(n ...
- 遇到bug如何处理
issue中查询是否有相似bug assert / try-except / IDE单步调式 框架可以查询源码或者查询官方文档