java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-servlet.xml]

杀马特。学长 韩版系。学妹 提交于 2019-12-08 04:14:23

问题


When I launch my application I get this error

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

Meanwhile, I dont have any file like SpringDispatcher-servlet.xml neither do I in my web.xml or mvc-dispatcher-servlet.xml file defined in my WEB-INF folder.

web.xml file

<context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <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>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

mvc-dispatcher-servlet.xml file

<context:component-scan base-package="aish.vaishno.musicstore.controller" />

    <mvc:annotation-driven />

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

Please how can I locate this file

/WEB-INF/SpringDispatcher-servlet.xml

Please what am I getting wrong?


回答1:


Spring is looking SpringDispatcher-servlet.xml in your web project and as it is unable to find it, it is throwing an exception.

You can override the dispatcher servlet xml file like this - providing blank arguments.

 <servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
  </servlet>



回答2:


When you define Dispatcher servlet in web.xml spring expects Web application context name as Disptacherservletname-servlet.xml under /WEB-INF/ . In your case it should be SpringDispatcher-servlet.xml not mvc-dispatcher-servlet.xml

Or you can use contextConfigLocation parameter to follow your own naming conventions.

<servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
  </servlet>



回答3:


In your web.xml you have define the servlet to be

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

</servlet>

therefore, you nee to create a file named SpringDispatcher-servlet.xml inside your WEB-INF folder. This is just how it works. If you change the servlet-name to be dispatcher then the file name should be dispatcher-servlet.xml.

Your SpringDispatcher-servlet.xml contains the definitions of your spring context. Take a look on this tutorial.



来源:https://stackoverflow.com/questions/36917191/java-io-filenotfoundexception-could-not-open-servletcontext-resource-web-inf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!