spring java.lang.IllegalStateException:未找到 WebApplicationContext:未注册 ContextLoaderListener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7704286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
提问by majda88
This the file web.xml in WEB-INF
这是 WEB-INF 中的文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>glpi.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
回答by Saket
I think you are missing the context loader listener(to pick your spring context file(s)).
我认为您缺少上下文加载器侦听器(选择您的 spring 上下文文件)。
Add this to your web.xml
将此添加到您的 web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You could also check out the Initial web configurationsection @ http://static.springsource.org/spring/docs/2.0.x/reference/beans.html
您还可以查看初始 Web 配置部分@ http://static.springsource.org/spring/docs/2.0.x/reference/beans.html
回答by Ryan Stewart
You have both ContextLoaderServlet and DispatcherServlet set to load-on-startup = 1. That means either one of them could start first, and you need the ContextLoaderServlet to start first, since that's what creates the root WebApplicationContext that your error says is missing. So leave ContextLoaderServlet's load-on-startup at 1, and change the DispatcherServlet's to 2 or higher.
您将 ContextLoaderServlet 和 DispatcherServlet 设置为 load-on-startup = 1。这意味着它们中的任何一个都可以先启动,并且您需要先启动 ContextLoaderServlet,因为这就是创建您的错误所说的缺少的根 WebApplicationContext 的原因。因此,将 ContextLoaderServlet 的 load-on-startup 保留为 1,并将 DispatcherServlet 的值更改为 2 或更高。
Actually, it's preferred to use ContextLoaderListenerinstead of the Servlet unless you're on a really old container where the Listener doesn't work properly.
实际上,最好使用ContextLoaderListener而不是 Servlet,除非您在一个非常旧的容器上,而 Listener 不能正常工作。
回答by Nailesh
Add following code in web.xml file, bcs it looks for contex to load so we have to declare it initially.
在 web.xml 文件中添加以下代码,bcs 它会查找要加载的上下文,因此我们必须首先声明它。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
回答by yktoo
I've recently stumbled upon the same issue, and I knew for sure it couldn't be caused by misconfiguration because I've copied the entire workingTomcat installation from another machine. Yet I kept getting the same exception:
我最近在同一问题迷迷糊糊的,我敢肯定,它不能被错误配置造成的,因为我已经复制了整个工作从另一台机器的Tomcat安装。但我一直收到同样的异常:
java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
As I've eventually figured out, it was a wrong JVM version that broke the application: this one used Java 7, whereas the working instance (and the webapp) was on Java 8.
正如我最终发现的那样,是一个错误的 JVM 版本破坏了应用程序:这个版本使用了Java 7,而工作实例(和 web 应用程序)使用的是Java 8。
Hope it helps someone struggling with this counter-intuitive error message.
希望它可以帮助那些在这个违反直觉的错误消息中挣扎的人。

