嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/spring/servlet-context.xml]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19402671/
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
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/spring/servlet-context.xml]
提问by Peter The Angular Dude
OK, thanks for helping out the last question. Here is my new error and that code that follows. The BOLD text in the error is all that I care about fixing since that is the only error.
好的,感谢您帮助解决最后一个问题。这是我的新错误和后面的代码。错误中的粗体文本是我关心的全部修复,因为这是唯一的错误。
[Thread-36] ContextLoader ERROR Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/spring/servlet-context.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/spring/servlet-context.xml]at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 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:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/spring/servlet-context.xml] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 19 more Oct 16, 2013 7:44:51 AM org.apache.catalina.core.StandardContext startInternal SEVERE: Error listenerStart Oct 16, 2013 7:44:51 AM org.apache.catalina.core.StandardContext startInternal SEVERE: Context [/mdimgmtsys-1.0-SNAPSHOT] startup failed due to previous errors
[Thread-36] ContextLoader 错误上下文初始化失败 org.springframework.beans.factory.BeanDefinitionStoreException: IOException 解析来自 ServletContext 资源 [/spring/servlet-context.xml] 的 XML 文档;嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/spring/servlet-context.xml]在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 在 org.springfactorybeans.beans. .support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) 在 org.springframework.beans.tractBeanDefinitionReader. .java:149) 在 org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124) 在 org.springframework.web。context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93) 在 org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) 在 org.springframework.context.support.AbstractApplicationContext.obtainFreshBean. 467) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397) 在 org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) 在 org.springframework.web.context.ContextLoader .initWebApplicationContext(ContextLoader.java:197) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204) at org.apache.catalina.core.StandardContext$1.call( StandardContext.java:5199) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)在 java.lang.Thread.run(Unknown Source) 引起:java.io.FileNotFoundException:无法在 org.springframework.web.context.support.ServletContextResource.getInputStream 打开 ServletContext 资源 [/spring/servlet-context.xml] (ServletContextResource.java:117) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader。loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 2013 年 10 月 16 日上午 7:44:51 org.apache.catalina.core.StandardContext startInternal 严重:错误 listenerStart 2013 年 10 月 16 日上午 7:44:51 org.apache .catalina.core.StandardContext startInternal 严重:由于先前的错误,上下文 [/mdimgmtsys-1.0-SNAPSHOT] 启动失败
Now the code:
现在代码:
<?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"
version="2.5">
<context-param>
<param-name>contextAppConfigLocation</param-name>
<param-value>app-context.xml classpath*:app-context.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/spring/servlet-context.xml classpath*:servlet-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Enables Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAppConfigLocation</param-name>
<param-value>app-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>contextServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<servlet-name>contextServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Everything I do fails to start the app in Tomcat.
我所做的一切都无法在 Tomcat 中启动应用程序。
回答by Dark Knight
as error says, it's an issue with servlet-context.xml file location. Change the path and it shall work fine.
正如错误所说,这是 servlet-context.xml 文件位置的问题。更改路径,它应该可以正常工作。
回答by xianlinbox
There are difference between Application Context and Servlet Context in a spring web project. we use to config Application Context, the usage the same as your usage
Spring Web 项目中的 Application Context 和 Servlet Context 是有区别的。我们用来配置Application Context,用法和你的用法一样
<context-param>
<param-name>contextAppConfigLocation</param-name>
<param-value>classpath:app-context.xml</param-value>
</context-param>
For Servlet Context, defaultly web container will try to find a #servlet-name#-servlet.xml in the WEB-INF directory, So for your problem, you need to move your to WEB-INF directory and change their name to corresponding names (contextServlet-servlet.xml, appServlet-servlet.xml)
对于 Servlet Context,默认 web 容器会尝试在 WEB-INF 目录中查找 #servlet-name#-servlet.xml,所以对于你的问题,你需要将你的移动到 WEB-INF 目录并将它们的名称更改为相应的名称(contextServlet-servlet.xml, appServlet-servlet.xml)
回答by sMajeed
I had resolved the same error by placing the servlet.xml
at correct location i.e. under WEB-INF
folder. You may try that as well.
我已经通过将servlet.xml
放在正确的位置(即WEB-INF
文件夹下)解决了同样的错误。你也可以试试。
回答by Alberto de Paola
If you want to change the location and naming convention for your spring configuration file, you should add a contextConfigLocation
to your DispatcherServlet
definition, for example:
如果要更改 spring 配置文件的位置和命名约定,则应contextConfigLocation
在DispatcherServlet
定义中添加一个,例如:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note that your app-context.xml
should be in the root of your src folder, else you have to specify the path in the classpath to it or the path in WEB-INF, without the classpath:
prefix.
请注意,您app-context.xml
应该在 src 文件夹的根目录中,否则您必须在类路径中指定路径或 WEB-INF 中的路径,不带classpath:
前缀。
For further details refer to this answer: https://stackoverflow.com/a/14956773/120794.
有关更多详细信息,请参阅此答案:https: //stackoverflow.com/a/14956773/120794。
回答by Juan Torres
I have the same problem a few months ago. This solve my problem using Maven instead of a downloaded version of Spring and some changes in the web.xml.
几个月前我也有同样的问题。这解决了我使用 Maven 而不是下载版本的 Spring 和 web.xml 中的一些更改的问题。
pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringWEB1</groupId>
<artifactId>SpringWEB1</artifactId>
<version>1</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Controller.java
控制器.java
package com.jmtm.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping("/hi")
public ModelAndView hi(){
return new ModelAndView("Hello", "msg", "Hello user.");
}
}
spring-servlet.xml
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<context:component-scan base-package="com.jmtm.controller"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
And the most important part of the solution, include the path of the servlet dispatcher (A.K.A. spring-servlet.xml) in the web.xml
解决方案中最重要的部分是在 web.xml 中包含 servlet 调度程序(AKA spring-servlet.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringWEB1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Something weird happen when I try to solve this. Any downloaded version of Spring from maven.springframework.org/release/org/springframework/springgives me a lot of problems (Tomcat couldn't find servlet, Spring stops Tomcat, Eclipse couldn't start the server {weird}) so with many problems find many partial solutions. I hope this works for you.
当我试图解决这个问题时,发生了一些奇怪的事情。从maven.springframework.org/release/org/springframework/spring下载的任何 Spring 版本都给我带来了很多问题(Tomcat 找不到 servlet,Spring 停止 Tomcat,Eclipse 无法启动服务器 {weird})所以与许多问题找到了许多部分解决方案。我希望这对你有用。
As an extra help, in Eclipse, download from the Eclipse Marketplace the Spring STS Tool for Eclipse, this will help you to create configuration files (servlet.xml) and write code for the servlet in the web.xml file.
作为额外的帮助,在 Eclipse 中,从 Eclipse Marketplace 下载 Spring STS Tool for Eclipse,这将帮助您创建配置文件 (servlet.xml) 并在 web.xml 文件中为 servlet 编写代码。