user2274307
user2274307

Reputation: 134

Spring MVC - context file not found

I use Apache Tomcat 7 and it's ON - Spring 3 with all the libraries on the classpath (compile and runtime).

that's my web.xml file in /WEB-INF/web.xml:

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

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

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

That's my context file in WEB-INF/dispatcherServlet-context.xml:

<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="controllers" />

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

the Tomcat error when i put http://localhost:8080/projectname/ in the browser is:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; 

nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml] ecc ecc ecc

note that the error concern the file dispatcherServlet-servlet.xml not the dispatcherServlet-context.xml (the one i use, created, and mapped)

Thank you all guys!

Upvotes: 0

Views: 5122

Answers (2)

Maksym Demidas
Maksym Demidas

Reputation: 7817

Just remove

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

snippet and rename your dispatcherServlet-context.xml to dispatcherServlet-servlet.xml. Cause: there are two different config files for Spring and Spring MVC. You have tried to use Spring MVC config by Spring Framework.

So for Spring : pass how many you want config files via `contextConfigLocation`
For Spring MVC : /WEB-INF/<dispatcher_servlet_name>-servlet.xml.

You can change default name for Spring MVC case (see @Stefan Lindenberg answer)

Upvotes: 1

Stefan
Stefan

Reputation: 12462

In Spring MVC the default location for a dispatcher servlet config is: /WEB-INF/[SERVLET-NAME]-servlet.xml. You can reconfigure this by adding an init parameter to your servlet declaration:

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            [PATH-TO-YOUR-FILE]/[CONFIG-FILE-NAME].xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Upvotes: 2

Related Questions