关于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. java亦或(^)

    在java程序里面的异或用法: 相同输出0,不同输出1,例如: System.out.println(1^1); 输出0 System.out.println(1^2):输出3,因为最后2个低位都不一 ...

  2. ### 七种SQL JOINS

    七种SQL JOINS 1.SELECT FROM TABLEA A LEFT JOIN TABLEB B ON A.Key=B.Key 2.SELECT FROM TABLEA A RIGHT JO ...

  3. X-NUCA 2017 web专题赛训练题 阳光总在风雨后和default wp

     0X0.前言 X-NUCA 2017来了,想起2016 web专题赛,题目都打不开,希望这次主办方能够搞好点吧!还没开赛,依照惯例会有赛前指导,放一些训练题让CTFer们好感受一下题目. 题目有一大 ...

  4. pip&easy_install使用

    pip install ... easy_install ... ******************************************************************* ...

  5. 【性能测试工具】- ApacheBench

    优点:提供的测试结果信息更完备:缺点:每次只能测试一个链接 概述: ab全称是ApacheBench,是 Apache 附带的一个HTTP性能测试小工具,可以同时模拟多个并发请求. 安装: Apach ...

  6. 【复制】【编码】MySQL复制中的编码问题

    编码背景知识 Latin-1,全称ISO 8859-1 Latin 1 对ASCII的拉丁语扩展 向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x8 ...

  7. C++11 中值得关注的几大变化(网摘)

    C++11 中值得关注的几大变化(详解) 原文出处:[陈皓 coolshell] 源文章来自前C++标准委员会的 Danny Kalev 的 The Biggest Changes in C++11 ...

  8. css复习内容

    有时候 自己动手写一遍比想十遍都有用 <!DOCTYPE html><html> <head> <meta charset="utf-8" ...

  9. 做的简单的一个静态web服务器,遇到个bug, 提示osError,这点一不小心就错了,特地记下来,加深记忆,socket须先绑定,再listen,如果是先listen再绑定,系统会自动分配一个端口,而程序绑定不了

    代码改正之前,先执行了listen,到了bind就报错:此程序只需将listen和改到bind后面即可 from socket import *from multiprocessing import ...

  10. 谈谈Golang中goroutine的调度问题

    goroutine的调度问题,同样也是我之前面试的问题,不过这个问题我当时并不是很清楚,回来以后立马查阅资料,现整理出来备忘. 有一些预备知识需要说明,就是操作系统中的线程.操作系统中的线程分为两种: ...