嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/spring/servlet-context.xml]

时间:2013-10-16 11:52:52

标签: java tomcat

好的,谢谢你帮我解决了最后一个问题。这是我的新错误和随后的代码。错误中的BOLD文本是我关心修复的全部内容,因为这是唯一的错误。

  

[Thread-36] ContextLoader错误上下文初始化失败   org.springframework.beans.factory.BeanDefinitionStoreException:从ServletContext资源解析XML文档的IOException [/spring/servlet-context.xml];嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/spring/servlet-context.xml]           在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)           在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)           在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)           在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)           在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)           在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)           在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)           at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)           在org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)           在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)           在org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)           在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)           在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)           在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)           at org.apache.catalina.core.StandardContext $ 1.call(StandardContext.java:5204)           at org.apache.catalina.core.StandardContext $ 1.call(StandardContext.java:5199)           at java.util.concurrent.FutureTask.run(Unknown Source)           at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)           在java.lang.Thread.run(未知来源)   引起:java.io.FileNotFoundException:无法打开ServletContext资源[/spring/servlet-context.xml]           在org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)           在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)           ......还有19个   2013年10月16日上午7:44:51 org.apache.catalina.core.StandardContext startInternal   严重:错误listenerStart   2013年10月16日上午7:44:51 org.apache.catalina.core.StandardContext startInternal   严重:由于先前的错误,上下文[/mdimgmtsys-1.0-SNAPSHOT]启动失败

现在代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">


<context-param>
    <param-name>contextAppConfigLocation</param-name>
    <param-value>app-context.xml classpath*:app-context.xml</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/spring/servlet-context.xml classpath*:servlet-context.xml</param-value>
</context-param>

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

<!-- Enables Spring Security -->
<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>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextAppConfigLocation</param-name>
        <param-value>app-context.xml</param-value>            
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>contextServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/spring/servlet-context.xml</param-value>

    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>          
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
    <servlet-name>contextServlet</servlet-name>
    <url-pattern>/</url-pattern>        
</servlet-mapping>    
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>        
</web-app>

我所做的一切都无法在Tomcat中启动应用程序。

5 个答案:

答案 0 :(得分:4)

Spring web项目中Application Context和Servlet Context之间存在差异。 我们使用配置应用程序上下文,用法与您的用法相同

<context-param>
    <param-name>contextAppConfigLocation</param-name>
    <param-value>classpath:app-context.xml</param-value>
</context-param>

对于Servlet Context,默认情况下,web容器将尝试在WEB-INF目录中找到#servlet-name#-servlet.xml。因此,对于您的问题,您需要将您的目录移至WEB-INF并更改其名称到相应的名称(contextServlet-servlet.xml,appServlet-servlet.xml)

答案 1 :(得分:0)

正如错误所说,这是servlet-context.xml文件位置的问题。改变路径和 它应该工作正常。

答案 2 :(得分:0)

我已将servlet.xml放在正确的位置(即WEB-INF文件夹下),从而解决了同样的错误。你也可以尝试一下。

答案 3 :(得分:0)

如果要更改spring配置文件的位置和命名约定,则应在contextConfigLocation定义中添加DispatcherServlet,例如:

<servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:app-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

请注意,您的app-context.xml应位于src文件夹的根目录中,否则您必须在其路径中指定路径,或者在WEB-INF中指定路径,而不指定classpath:前缀。

有关详细信息,请参阅此答案:https://stackoverflow.com/a/14956773/120794

答案 4 :(得分:0)

几个月前我遇到了同样的问题。这解决了我使用Maven而不是Spring的下载版本以及web.xml中的一些更改的问题。

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpringWEB1</groupId>
  <artifactId>SpringWEB1</artifactId>
  <version>1</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>
</project>

Controller.java

package com.jmtm.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@org.springframework.stereotype.Controller
public class Controller {

    @RequestMapping("/hi")
    public ModelAndView hi(){
        return new ModelAndView("Hello", "msg", "Hello user.");
    }

}

弹簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

    <context:component-scan base-package="com.jmtm.controller"></context:component-scan>

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

</beans>

解决方案中最重要的部分,包括web.xml中servlet调度程序的路径(A.K.A. spring-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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>SpringWEB1</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
当我试图解决这个问题时,会发生一些奇怪的事情。从maven.springframework.org/release/org/springframework/spring下载的任何Spring版本都给我带来了很多问题(Tomcat无法找到servlet,Spring会停止Tomcat,Eclipse无法启动服务器{怪异})所以很多问题都发现很多部分解决方案我希望这适合你。

作为额外的帮助,在Eclipse中,从Eclipse Marketplace下载Spring STS Tool for Eclipse,这将帮助您创建配置文件(servlet.xml)并在web.xml文件中为servlet编写代码。 / p>