【问题标题】:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-servlet.xml]java.io.FileNotFoundException:无法打开 ServletContext 资源 [/WEB-INF/SpringDispatcher-servlet.xml]
【发布时间】:2023-03-15 11:15:01
【问题描述】:

当我启动我的应用程序时出现此错误

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]

同时,我的 web.xml 或 WEB-INF 文件夹中定义的 mvc-dispatcher-servlet.xml 文件中也没有 SpringDispatcher-servlet.xml 之类的文件。

web.xml 文件

<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 文件

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

请问我怎样才能找到这个文件

/WEB-INF/SpringDispatcher-servlet.xml

请问我哪里错了?

【问题讨论】:

    标签: spring spring-mvc servlets configuration-files


    【解决方案1】:

    Spring 正在您的 Web 项目中查找 SpringDispatcher-servlet.xml,但由于无法找到它,因此引发了异常。

    您可以像这样覆盖调度程序 servlet xml 文件 - 提供空白参数。

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

    【讨论】:

    • 添加时,它会抛出资源未找到
    • 能否请您添加完整的堆栈跟踪?
    • 信息:初始化 Spring FrameworkServlet 'SpringDispatcher'
    【解决方案2】:

    当您在 web.xml 中定义 Dispatcher servlet 时,spring 期望 Web 应用程序上下文名称为 /WEB-INF/ 下的 Disptacherservletname-servlet.xml。 在您的情况下,它应该是 SpringDispatcher-servlet.xml 而不是 mvc-dispatcher-servlet.xml

    或者您可以使用 contextConfigLocation 参数来遵循您自己的命名约定。

    <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】:

      在您的 web.xml 中,您已将 servlet 定义为

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

      因此,您需要在 WEB-INF 文件夹中创建一个名为 SpringDispatcher-servlet.xml 的文件。这就是它的工作原理。如果将 servlet-name 更改为 dispatcher,则文件名应为 dispatcher-servlet.xml。

      您的 SpringDispatcher-servlet.xml 包含您的弹簧上下文的定义。看看这个tutorial

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2018-12-18
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 2016-04-04
        • 2014-04-16
        • 1970-01-01
        相关资源
        最近更新 更多