Java 如何解析BeanDefinitionStoreException:IOException解析来自ServletContext资源的XML文档[/WEB-INF/dispatcher servlet.XML]?

Java 如何解析BeanDefinitionStoreException:IOException解析来自ServletContext资源的XML文档[/WEB-INF/dispatcher servlet.XML]?,java,spring,spring-mvc,web-inf,Java,Spring,Spring Mvc,Web Inf,错误堆栈跟踪: 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.FileNotFoundExcepti

错误堆栈跟踪:

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

<?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 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

<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>

调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
WEB-INF/dispatcher-servlet.xml
1.
调度员
/
上下文配置位置
WEB-INF/dispatcher-servlet.xml
/search.jsp
我尝试了网上给出的几种解决方案,比如 1) 对所有文件授予读写权限-不起作用 2) 添加init param-不起作用 3) 将dispatcherservlet的路径显式指定为/WebContent/WEB-INF/dispatcher-servlet.xml-不起作用

我在ApacheTomcat服务器v7.0上运行项目时遇到此错误

在过去的4天里,我一直在思考这个问题。请帮帮我

以下是有效的解决方案

您的war没有dispatcher-servlet.xml。1) 该项目没有webapp文件夹。当您使用maven创建项目时,请注意。您可以按照这里提到的步骤操作 您可能会使用一些奇怪的maven原型创建项目。尝试上面的链接,它的工作


我在internet的其他位置找不到此解决方案。:)

尝试在WEB.xml中的
/WEB-INF/dispatcher servlet.xml
上更改
WEB-INF/dispatcher servlet.xml
首先,dispatcher-servlet.xml是spring mvc的配置,不适用于应用。因此,您应该删除web.xml中的标记

然后SpringMVC将根据servlet名称dispatcher获取xml文件。因此,您还可以删除web.xml中的标记


试试看SpringMVC是否能自动找到xml,spring为web应用程序创建了两个应用程序上下文。第一个是包含应用程序bean(例如DAOs服务对象等)的根应用程序上下文。此上下文(在您的示例中为applicationContext.xml)是使用context param contextConfigLocation配置的。第二个是包含特定于web的bean的子web应用程序上下文。这是使用DispatcherServlet的init param contextConfiguration配置的

在您的例子中,您已经为这两个文件复制了配置文件。更改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>

调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
1.
调度员
/
上下文配置位置
/WEB-INF/applicationContext.xml
/search.jsp

替换mvc配置文件uri,如下所示:

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value>
</context-param>

上下文配置位置
WEB-INF/classes/dispatcher-servlet.xml

确保bean标记中声明的所有XSD都在该位置可用。

@SotiriosDelimanolis对此表示抱歉。现在编辑了文件。是的,我也试过这个。不走运,有趣的是,不管我添加/不添加,堆栈跟踪总是/在其中。尝试在web.xml
org.springframework.web.context.ContextLoaderListener中添加此侦听器也尝试过。它引发applicationContext.xml缺少IOException。那怎么修好呢。添加它并不能解决dispatcher-servlet.xml问题。好的,请将路径写入dispatcher-servlet.xml。您所说的路径是什么意思?这对我很有用!几个小时后……终于!但它在浏览器中作为url启动。我的意思是,它不应该启动中提到的jsp页面吗?