Java 无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22350721/
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
Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
提问by Leos Literak
Ok, I am 500th user asking this question, I read many answersbut still having no luck.
好的,我是第 500 个问这个问题的用户,我阅读了很多答案,但仍然没有运气。
parent module pom contains:
父模块 pom 包含:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.framework.version}</version>
</dependency>
Child module has maven-jetty-plugin
and I run my webapp module with jetty:run
.
子模块有maven-jetty-plugin
,我用jetty:run
.
web.xml
defines standard dispatcher module:
web.xml
定义标准调度模块:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I have file dispatcher-servlet.xml
under WEB-INF
, though start fails with:
我在dispatcher-servlet.xml
下有文件WEB-INF
,但启动失败:
FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
What is wrong? Documentation and everybody says that Spring MVC will search for XX-servlet.xml, where XX is name of servlet. Why does it search for applicationContext.xml
?
怎么了?文档和大家都说Spring MVC会搜索XX-servlet.xml,其中XX是servlet的名字。它为什么要搜索applicationContext.xml
?
采纳答案by gerrytan
ContextLoaderListener
has its own context which is shared by all servlets and filters. By default it will search /WEB-INF/applicationContext.xml
ContextLoaderListener
有自己的上下文,由所有 servlet 和过滤器共享。默认情况下它会搜索/WEB-INF/applicationContext.xml
You can customize this by using
您可以使用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/somewhere-else/root-context.xml</param-value>
</context-param>
on web.xml
, or remove this listener if you don't need one.
on web.xml
,或者如果您不需要,则删除此侦听器。
回答by Aalkhodiry
Update:This will create a second context same as in applicationContext.xml
更新:这将创建与 applicationContext.xml 中相同的第二个上下文
or you can add this code snippet to your web.xml
或者您可以将此代码片段添加到您的web.xml
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
instead of
代替
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>