java 未找到 WebApplicationContext:未注册 ContextLoaderListener?然后404错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13719247/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:45:30  来源:igfitidea点击:

No WebApplicationContext found: no ContextLoaderListener registered? and then 404 error

javaformsspringspring-mvc

提问by Freakyuser

I found that the error was due to the addition of the following lines in my jsp

我发现错误是由于在我的jsp中添加了以下几行

<form:form></form:form>

I checked these two posts relevant to my error.

我检查了这两个与我的错误相关的帖子。

No WebApplicationContext found: no ContextLoaderListener registered?

未找到 WebApplicationContext:未注册 ContextLoaderListener?

spring security No WebApplicationContext found

spring security 找不到 WebApplicationContext

In both of the posts they have mentioned to add this code

在他们提到的两个帖子中添加此代码

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>  

After adding this code in my web.xml like this

像这样在我的 web.xml 中添加此代码后

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringSort</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <servlet>
        <servlet-name>sort</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sort</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

I am getting HTTP Status 404 - error and my project is not available(that is what the server says).

我收到 HTTP 状态 404 - 错误并且我的项目不可用(服务器就是这么说的)。

The relevant jars I have in my build path are: 1.springwebmvc3.0.3.jar, 2.spring2.5.6.jar, and I have the spring-form.tld file also.

我在构建路径中的相关 jar 是:1.springwebmvc3.0.3.jar、2.spring2.5.6.jar,我也有 spring-form.tld 文件。

How to solve this error?
Should I add some more jar files?
I am using STS 2.9.2 release for development purpose. Below is the start up log from the console.

如何解决这个错误?
我应该添加更多 jar 文件吗?
我正在使用 STS 2.9.2 版本进行开发。下面是控制台的启动日志。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    ... 27 more

Thanks in Advance

提前致谢

采纳答案by Freakyuser

The 404 error was due to the fact that I was missing context-param tag.
So I added this code to my web.xml

404 错误是由于我缺少上下文参数标记。
所以我将此代码添加到我的 web.xml

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value> 
</context-param>

After adding it I got the following error:

添加后,我收到以下错误:

javax.servlet.ServletException: Servlet.init() for servlet sort threw exception
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)...

java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:430)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)...

Getting this error I found that I was mixing a jar file in my lib folder. From some input I found that the culprit jar was spring2.5.6.jar.
So, I deleted the spring2.5.6.jar.

得到这个错误我发现我在我的 lib 文件夹中混合了一个 jar 文件。从一些输入中我发现罪魁祸首 jar 是 spring2.5.6.jar。
所以,我删除了spring2.5.6.jar。

The above steps did the trick... Thanks to all the inputs.

上面的步骤成功了……感谢所有的投入。

回答by NimChimpsky

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

There's you problem ... you have not got a correctly named, and located applicationContext.xml. Where are you saving the applicationContext.xml, have you named it correctly ?

你有问题……你没有正确命名和定位applicationContext.xml。您在哪里保存 applicationContext.xml,您是否正确命名了它?