0
votes

I have simple spring project using spring mvc. I have changed the dispatcher servlet name but mvn install fails with error.

Error stack trace : 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]

1)web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0" >

 <display-name>Order Activation Snapshot</display-name>

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


    <!-- Loads Spring Security config file -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>        
            /WEB-INF/spring-security.xml,
            /WEB-INF/data-source-cfg.xml,
            /WEB-INF/mvc-dispatcher-servlet.xml
        </param-value>

    </context-param>


    <!-- Spring MVC -->
    <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>/</url-pattern>
    </servlet-mapping>


    <!-- Spring Security Filter -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

2)mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
      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-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd">

      <context:component-scan base-package="org.o7planning.tutorial.springmvcsecurity.controller" />
      <context:annotation-config />

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

    </beans>

Not sure what I am missing here.

File structure in my code base

1
Do you have any test cases also in your applicaition?Vijendra Kumar Kulhade

1 Answers

0
votes

Your dispatcher servlet name should be unique. You likely told your compiler/Intepreter somewhere in your code(haven't seen it here) to look for dispatcher-servlet.xml at /WEB-INF/. But seems you named your dispatcher with mvc-dispatcher-servlet.xml. The java.io.FileNotFoundException exception might be as a result. Two things: Either you go to where you had dispatcher-servlet.xml and replace it with mvc-dispatcher-servlet.xml or you do the following:

Replace your dispatcher servlet name:

mvc-dispatcher-servlet

with

dispatcher-servlet

everywhere.

Add .xml extension to dispatcher-servlet where need be.

For instance, in your web.xml, you can do the following changes:

...

<!-- Loads Spring Security config file -->
<context-param>
    <param-name>contextConfigLocation</param-name>
     <param-value>        
        /WEB-INF/spring-security.xml,
        /WEB-INF/data-source-cfg.xml,
        /WEB-INF/dispatcher-servlet.xml <!--changed here -->
    </param-value>

</context-param>


<!-- Spring MVC -->
<servlet>
    <servlet-name>dispatcher</servlet-name> <!--changed here -->
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name> <!--changed here -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

...

Note. I commented lines I made change on.