关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx.xml]

问题描述:

在搭建SpringMVC项目时,使用多个Spring配置文件,这里我在web.xml中配置spring的配置文件:

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-mvc.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>

然后再spring-mvc.xml配置文件中又使用<import>标签导入其他的配置文件:applicationContext.xml,导入代码如下:

<import resource="./applicationContext.xml"/>

其中我将两个spring配置文件都放在resources下的spring文件夹下,文件结构如下:

|---resources
|---spring
|---spring-mvc.xml
|---applicationContext.xml

此时在项目执行时报错:

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

解决办法

看到这个错误,我是不相信的,我检查了我的spring-mvc.xml文件,确定我使用是相对路径,并没有什么问题,于是到网上查资料,说使用spring配置文件路径填写的不对,推荐使用:classpath:或者classpath*:,分别使用这两种方式在<import>中改写applicationContext.xml的路径,可以是依然报FileNotFoundException,我十分确定我填写的路径没有问题,但是一直报错就十分奇怪,我观察报错的路径:

Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

项目在运行是是直接去WEB-INFapplicationContext.xml文件,我查看项目运行时target文件夹,找到这是applicationContext.xml配置文件在:

/WEB-INF/classes/spring/applicationContext.xml

下,于是我将applicationContext.xml放到WEB-INF下在执行,发现没有的报错了,说明项目在运行时<import>根本没有起到应有的作用,系统依然走的是默认到WEB-INF目录下找的配置文件,这显然不是我要的效果,就在我最抓狂的时候,我发现我的web.xml少了几行配置:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-mvc.xml</param-value>
</context-param>

抱着试一试的想法,我将这一行配置加上,运行项目后顺利运行

......

......

......

(╯‵□′)╯︵┻━┻

总结

经过这一次的教训,发现做事的时候还是不够细心,这里我在晚上寻找解决办法是有关这个问题需要注意的地方列一下:

  • 经过多方资料的对比,在设置spring配置文件时,建议使用classpath而不是classpath*貌似会因为这个问题报错,我暂时没有遇到过
  • 在配置文件wen.xml中使用<context-param>配置contextConfigLocation变量,我就是犯了这个错误
  • 配置文件名一定要写写对,这种属于最低级也是最容易忽略的错误

另外如果有什么其他问题,或者错误的地方,希望大家提出宝贵的意见,谢谢

参考资料

http://blog.csdn.net/lee0723/article/details/18223749

http://perfy315.iteye.com/blog/2009258

http://blog.csdn.net/zwx_20092029/article/details/39229971

关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx.xml]的更多相关文章

  1. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/spring/applicationContext.xml]

    在搭建SpringMVC框架的时候遇到了这个问题 问题的原因: 就是没有找到applicatoincontext.xml这个文件, 因为idea自动生成的路径不正确 因此需要再web.xml里面, ( ...

  2. 文件上传报错java.io.FileNotFoundException拒绝访问

    局部代码如下: File tempFile = new File("G:/tempfileDir"+"/"+fileName); if(!tempFile.ex ...

  3. 完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径。)

    完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径.) 错误原因 读出文件的路径需要有被拷贝的文件名,否则无法解析地址 源代码(用 ...

  4. 出错: IOException parsing XML document from ServletContext resource [/cn.mgy.conig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/cn.mgy.conig]

    错误的详细内容: 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanDefinitionStoreExceptio ...

  5. IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/app

    web.xml初始化spring容器出错 org.springframework.beans.factory.BeanDefinitionStoreException: IOException par ...

  6. 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist

    不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句:  ./spark-shell  --master spark://ma ...

  7. Spark启动报错|java.io.FileNotFoundException: File does not exist: hdfs://hadoop101:9000/directory

    at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:) at org.a ...

  8. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

    这是因为我把 [/WEB-INF/dispatcher-servlet.xml]的位置换成了[config/springmvc/dispatcher-servlet.xml] 因此idea在原来的位置 ...

  9. idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8

    问题如上面所叙: > idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8 解决方案: > Setting->Compiler->Ja ...

随机推荐

  1. Vue实例对象的数据选项

    前面的话 一般地,当模板内容较简单时,使用data选项配合表达式即可.涉及到复杂逻辑时,则需要用到methods.computed.watch等方法.本文将详细介绍Vue实例对象的数据选项 data ...

  2. Selenium对于对话框alert,confirm,prompt的处理

    html 源码: <html>                  <head>                      <title>Alert</titl ...

  3. Python打包EXE神器 pyinstaller

    最近由于项目需要,以前的python文件需要编辑为EXE供前端客户使用. 由于最早接触的是distutils,所以一开始准备使用distutils和py2exe搭配来进行python的exe化,也就是 ...

  4. 从SVN到Git最强指南

    对于软件开发人员来说,版本控制系统他们再熟悉不过了,所谓版本控制系统就是软件项目开发过程中用于储存开发人员所写代码所有修订版本的软件.它的主要目的是实现开发团队并行开发.提高开发效率,对软件开发进程中 ...

  5. javascript面向对象属性函数用法(defineProperty与getOwnPropertyDescriptor)

    defineProperty用于设置一个对象的属性描述符,属性描述符有4个:[[Configurable]], [[Enumerable]], [[Writable]],[[Value]] 当一个属性 ...

  6. CSS常见英语单词属性一览

    这些是css中常会用到的一些英文单词,大家可以多看看,多使用就会容易记得了. color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体 ...

  7. MySQL触发器学习

    简介 MySQL从5.0.2版本开始支持触发器的功能.触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合. 创建触发器 语法: CREATE TRIGGER trigge ...

  8. Go基础

    Go编程基础 package 别名 当使用第三方包时,包名可能会非常接近或者相同,此时就可以使用别名来进行区别和调用 //当前程序的包名 package main //导入其他的包 import &q ...

  9. js面试题知识点全解(一原型和原型链1)

    1.如何准确判断一个变量是数组类型2.写一个原型链继承的例子3.描述new一个对象的过程4.zepto(或其他框架)源码中如何使用原型链知识点:1.构造函数2.构造函数-扩展3.原型规则和示例4.原型 ...

  10. logstash结合zabbix报警安装部署

    cd /usr/share/logstash/ vim Gemfile source "https://ruby.taobao.org/" ##修改成国内镜像站 source &q ...