无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml] [英] Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

查看:37
本文介绍了无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我是问这个问题的第 500 个用户,我阅读了很多答案 但仍然没有运气.

Ok, I am 500th user asking this question, I read many answers but still having no luck.

父模块 pom 包含:

parent module pom contains:

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

子模块有 maven-jetty-plugin,我用 jetty:run 运行我的 webapp 模块.

Child module has maven-jetty-plugin and I run my webapp module with jetty:run.

web.xml 定义了标准的调度模块:

web.xml defines standard dispatcher module:

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

我在 WEB-INF 下有文件 dispatcher-servlet.xml,但启动失败:

I have file dispatcher-servlet.xml under WEB-INF, though start fails with:

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

怎么了?文档和大家都说Spring MVC会搜索XX-servlet.xml,其中XX是servlet的名字.为什么要搜索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?

推荐答案

ContextLoaderListener 有自己的上下文,所有 servlet 和过滤器都共享该上下文.默认情况下它会搜索 /WEB-INF/applicationContext.xml

ContextLoaderListener has its own context which is shared by all servlets and filters. By default it will search /WEB-INF/applicationContext.xml

您可以使用此自定义

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/somewhere-else/root-context.xml</param-value>
</context-param>

web.xml 上,或者如果你不需要的话,删除这个监听器.

on web.xml, or remove this listener if you don't need one.

这篇关于无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆