java 无法打开 ServletContext 资源 [/WEB-INF/mvc-dispatcher-servlet.xml]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33820507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:09:44  来源:igfitidea点击:

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

javaxmlspringservlets

提问by Programmer254

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

运行代码时出现此错误。任何帮助将不胜感激。谢谢。

Here is my dispatcher servlet.

这是我的调度程序 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

这是我的 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

回答by Kamill Sokol

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.

Spring 无法找到您的调度程序配置。你没有定义一个contextConfigLocation. 在这种情况下,Spring 会尝试从/WEB-INF/<servlet-name>-servlet.xml. 在你的情况下/WEB-INF/mvc-dispatcher-servlet.xml

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

要么将您的配置放在下面/WEB-INF/并将其重命名为,mvc-dispatcher-servlet.xml要么contextConfigLocation在您的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>

回答by Beri

You have 2 solutions:

您有 2 个解决方案:

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

输入您的上下文配置文件(在我的示例中为 applicationContext.xml),通过添加:

<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.xmlin yout WEB-INF. The rule is simple, spring is looking for a file according to a pattern: {servletName}-servlet.xml

如果您的服务器名为 mvc-application,spring 将循环查找名为mvc-application-servlet.xmlyout的文件 WEB-INF。规则很简单,spring 正在根据一个模式寻找一个文件:{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'

所以你可以重命名你的文件,或者直接设置它的路径。最简单的方法是将您的配置文件重命名为:'mvc-dispatcher-servlet.xml'

回答by JustCode

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.

我也遇到了同样的问题。我认为您需要为 web.xml 文件添加一些新内容。这是我最终正确的 web.xml 文件线。

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

servlet-name,servlet-mapping,param-value 相同。

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

否则显示错误。这里是mvc-dispatcher。然后将 servlet.xml 文件具体化为mvc-dispatcher-servlet.xml

You can get more idea from thisdoc.

你可以从这个文档中得到更多的想法。