11

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

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

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.

I get this error when I run the project on Apache tomcat server v7.0

I am struck with this problem for past 4 days. Please help me.

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.

I don't find this solution else where in internet. :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
sofs1
  • 3,834
  • 11
  • 51
  • 89
  • @SotiriosDelimanolis Sorry about that. Edited the file now. – sofs1 Sep 15 '14 at 17:52
  • Hi @sofs1, Glad you've found a solution for this. The link you've provided is not working. Could you please detail the solution in the comment section below? Thanks – Techie Apr 04 '23 at 23:05

5 Answers5

5

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.

In your case you have duplicated the config file for both. Change your web.xml as follows

<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>
ekem chitsiga
  • 5,523
  • 2
  • 17
  • 18
  • This works for me! After hours..finally! But this launches http://localhost:8080/MyTestWebApp/WEB-INF/web.xml as url in the browser. I mean, shouldn't it launch the the jsp pages mentioned in ? – Tanuj Dhaundiyal May 25 '16 at 12:38
0

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

Alexey Semenyuk
  • 3,263
  • 2
  • 32
  • 36
  • Yeah, I also tried this. No luck and the interesting thing is, either I add / or not, the stack trace has always / in it. – sofs1 Sep 15 '14 at 18:23
  • Try to add this listener in web.xml ` org.springframework.web.context.ContextLoaderListener ` – Alexey Semenyuk Sep 15 '14 at 18:35
  • Tried that as well. It threw applicationContext.xml missing IOException. Then some how fixed it. Adding that doesn't solve this dispatcher-servlet.xml issue. – sofs1 Sep 15 '14 at 18:42
  • Ok, please, write path to your dispatcher-servlet.xml. – Alexey Semenyuk Sep 15 '14 at 18:56
  • What you mean by path? – sofs1 Sep 15 '14 at 19:32
  • Your structure folders to dispatcher-servlet.xml. – Alexey Semenyuk Sep 15 '14 at 19:40
  • Should be something like this \src\main\webapp\WEB-INF\dispatcher-servlet.xml. Do you have such as this in your project? – Alexey Semenyuk Sep 15 '14 at 19:44
  • I have \WEB-INF\dispatcher-servlet.xml in my project. – sofs1 Sep 15 '14 at 20:07
  • What's structure folders on tomcat after deploy? – Alexey Semenyuk Sep 15 '14 at 20:11
  • May be your problem related with broken folder structure. Spring searchs dispatcher-servlet.xml in other place. Should be (name_your_project)\src\main\webapp\WEB-INF\dispatcher-servlet.xml and after deploy your war should be apache-tomcat\webapps\(name_your_war)\WEB-INF\dispatcher-servlet.xml. For example look typical app: https://github.com/manishchhabra/HelloSpringWithMongoDB – Alexey Semenyuk Sep 15 '14 at 20:24
  • How to fix the broken structure? I found out that dispatcher-servlet.xml and applicationContext.xml doesn't get included in the war that is built. – sofs1 Sep 16 '14 at 01:56
  • Need to turn to the correctly structure project, for example, dispatcher-servlet.xml should be in (name_your_project)\src\main\webapp\WEB-INF\dispatcher-servlet.xml, etc. For more details how should be look, for example, https://github.com/manishchhabra/HelloSpringWithMongoDB. Also pay attention on classpath in your project, that you can look in IDE. – Alexey Semenyuk Sep 16 '14 at 08:05
  • Also pay attention on content web.xml, for example, not need twice writting contextConfigLocation, so you can remove ` contextConfigLocation WEB-INF/dispatcher-servlet.xml ` In other words looking and doing structure and content such in example and you will have less problem. – Alexey Semenyuk Sep 16 '14 at 08:06
0

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.

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

try it to see if spring mvc can find the xml auto

xierui
  • 1,047
  • 1
  • 9
  • 22
0

make sure all the xsds declared at the beans tag are available at the location.

-1

Replace your mvc-config file uri as below:

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value>
</context-param>
arghtype
  • 4,376
  • 11
  • 45
  • 60
M.c
  • 1