Programmer254
Programmer254

Reputation: 41

Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]

I am having this error when I run my code. Any help will be appreciated. Thanks.

Here is my dispatcher servlet.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="tutorial.mvc"/>

    <mvc:annotation-driven/>

    <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">      
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />    
    </bean> 
</beans>

Here is my web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app> 

Here is the error that I end up getting.

     Nov 20, 2015 1:39:45 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
    ... 35 more

Upvotes: 0

Views: 14083

Answers (4)

I came across this issue when I couldn't launch quite old code inside Tomcat 9. Versions are important, as they are Java dependent. For tomcat 9 at least java 8 or more. There were 2 additional steps to what was described in this thread, I had to make to get a working project:

-update spring jars,

-get correct folders structure in project and war file;

Current Spring MVC version is 5.3.14 https://mvnrepository.com/artifact/org.springframework/spring-webmvc Spring MVC version of the project that I was working on was 3.0.3.RELEASE The first thing I had to do was to update Spring from 3 to 4. To do that, I switched from Ant to Maven. But the project was not working, so I had to adjust the structure of the folders inside the project.

Doing my research, I understood that the way folders are organized inside the WAR is crucial, but default settings inside 'Maven' didn't allow building the necessary structure.

I used: maven-war-plugin 3.3.2 to achieve necessary structure in my project. Below I will share the structure that worked for me:

- The structure of the project(inside IDE)

- The structure of the project(inside war file that was produced)

Upvotes: 0

Priyantha
Priyantha

Reputation: 5083

I also had the same problem like this.I think you need to add some new thing for the web.xml file.Here is my final correct cord for web.xml file.

  <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <!-- <url-pattern>/services/*</url-pattern> -->
     <url-pattern>/</url-pattern>
   </servlet-mapping>

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

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

According to this

servlet-name,servlet-mapping,param-value same.

Otherwise it is show error.Here is mvc-dispatcher. Then you crete your servlet.xml file as mvc-dispatcher-servlet.xml

You can get more idea from this doc.

Upvotes: 0

Beri
Beri

Reputation: 11600

You have 2 solutions:

Enther name your context configuration file (applicationContext.xml in my example), by adding :

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

You can also use a file from your classpath (resources).

Or use the default mechanism:

If your servler is names mvc-application, spring will loop for a file named mvc-application-servlet.xml in yout WEB-INF. The rule is simple, spring is looking for a file according to a pattern: {servletName}-servlet.xml

So you can either rename your file, or set it's path directly. Simplest aproatch is to rename your configuration file to : 'mvc-dispatcher-servlet.xml'

Upvotes: 0

user3151168
user3151168

Reputation:

Spring is unable to find your dispatcher configuration. You didn't define a contextConfigLocation. In that case Spring tries to load a dispatcher configuration from /WEB-INF/<servlet-name>-servlet.xml. In your case /WEB-INF/mvc-dispatcher-servlet.xml.

Either you put your configuration under /WEB-INF/ and rename it to mvc-dispatcher-servlet.xml or you define a contextConfigLocation in your web.xml:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/points/to/your/configuration.xml</param-value>
    </init-param> 
</servlet>

Upvotes: 2

Related Questions