Java 如何解决 BeanDefinitionStoreException: IOException 解析来自 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 的 XML 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25853804/
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
How to resolve BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]?
提问by sofs1
Error Stack trace:
错误堆栈跟踪:
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
dispatcher-servlet.xml
调度程序-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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task/spring-task.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.exam.www" />
<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="Results" />
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp"
p:suffix=".jsp" />
<!-- <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
<bean class=
"org.springframework.web.servlet.view.tiles2.TilesConfigurer"> -->
<!-- <property name="definitions">
<list>
<value>/WEB-INF/views/views.xml</value>
</list>
</property>
</bean> -->
</beans>
applicationContext.xml
应用上下文.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!--
CSRF protection. Here we only include the CsrfFilter instead of all of Spring Security.
See http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf for more information on
Spring Security's CSRF protection
-->
<!-- <bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter">
<constructor-arg>
<bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/>
</constructor-arg>
</bean> -->
<!--
Provides automatic CSRF token inclusion when using Spring MVC Form tags or Thymeleaf. See
http://localhost:8080/#forms and form.jsp for examples
-->
<!-- <bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor"/>
-->
</beans>
web.xml
网页.xml
<web-app 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"
version="2.5">
<!-- <display-name>Spring With MongoDB Web Application</display-name> -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/search.jsp</welcome-file>
</welcome-file-list>
</web-app>
I tried several solutions given online like 1) Giving read, write permissions to all files - Doesn't work 2) Adding init-param - doesn't work 3) Explicitly giving path of dispatcherservlet as /WebContent/WEB-INF/dispatcher-servlet.xml - doesn't work.
我尝试了几种在线提供的解决方案,例如 1) 授予对所有文件的读、写权限 - 不起作用 2) 添加 init-param - 不起作用 3) 明确地将 dispatcherservlet 的路径指定为 /WebContent/WEB-INF/dispatcher- servlet.xml - 不起作用。
I get this error when I run the project on Apache tomcat server v7.0
在 Apache tomcat 服务器 v7.0 上运行项目时出现此错误
I am struck with this problem for past 4 days. Please help me.
过去 4 天我对这个问题感到震惊。请帮我。
Following is the solution that worked.
以下是有效的解决方案。
Your war doesn't have the dispatcher-servlet.xml. 1) The project doesn't have webapp folder. When you create a project using maven be mindful. You can follow the steps mentioned here http://blog.manishchhabra.com/2013/04/spring-data-mongodb-example-with-spring-mvc-3-2/You would have created the project using some strange archetype of maven. Try the above link and it works.
您的战争没有 dispatcher-servlet.xml。1) 该项目没有 webapp 文件夹。当您使用 maven 创建项目时要注意。你可以按照这里提到的步骤 http://blog.manishchhabra.com/2013/04/spring-data-mongodb-example-with-spring-mvc-3-2/你会使用一些奇怪的原型创建项目行家。试试上面的链接,它的工作原理。
I don't find this solution else where in internet. :)
我在互联网上的其他地方找不到这个解决方案。:)
回答by Alexey Semenyuk
Try to change <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
on <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
in web.xml
尝试改变<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
在<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
web.xml中
回答by xierui
First, dispatcher-servlet.xml is the config of spring mvc, it is not for applicatoin. So you should delete the < context-param > tag in your web.xml.
首先dispatcher-servlet.xml是spring mvc的配置,不是applicatoin的。所以你应该删除 web.xml 中的 <context-param> 标签。
Then spring mvc will get the xml file according the servlet-name dispatcher. So you can also delete the tag < init-param > in web.xml < servlet >.
然后spring mvc会根据servlet-name dispatcher获取xml文件。所以你也可以删除web.xml<servlet>中的标签<init-param>。
try it to see if spring mvc can find the xml auto
试试看spring mvc能不能找到xml auto
回答by ekem chitsiga
Spring creates two application contexts for web application. The first is the root application context containing application beans e.g DAOs service objects etc. This context (applicationContext.xml in your case) is configured using context-param contextConfigLocation. The second one is the child web application context containing your web pecific beans. This is configured using the init-param contextConfiguration of the dispatcher servlet.
Spring 为 Web 应用程序创建了两个应用程序上下文。第一个是包含应用程序 bean 的根应用程序上下文,例如 DAO 服务对象等。此上下文(在您的情况下为 applicationContext.xml)是使用上下文参数 contextConfigLocation 配置的。第二个是包含 Web 特定 bean 的子 Web 应用程序上下文。这是使用调度程序 servlet 的 init-param contextConfiguration 配置的。
In your case you have duplicated the config file for both. Change your web.xml as follows
在您的情况下,您已为两者复制了配置文件。更改您的 web.xml 如下
<web-app 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"
version="2.5">
<!-- <display-name>Spring With MongoDB Web Application</display-name> -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/search.jsp</welcome-file>
</welcome-file-list>
</web-app>
回答by M.c
Replace your mvc-config file uri as below:
替换您的 mvc-config 文件 uri,如下所示:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value>
</context-param>