我正在尝试研究和修改示例项目。我首先尝试将其与现有的 Google OAuth 项目“合并”,然后出现上述错误。
我对 Spring 完全是个菜鸟,但显然这告诉我它找不到它应该找到的东西(在本例中是 security-context.xml)。
看起来有点不正统(网上的例子通常把它放在/WEB-INF/Spring中),但是...这是目录结构(在Spring Tool Suite中):
这是我的 web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:WEB-INF/spring/root-context.xml
/src/main/resources/security-context.xml
</param-value>
<!-- <param-value>/WEB-INF/spring/root-context.xml</param-value> -->
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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:WEB-INF/spring/appServlet/servlet-context.xml</param-value>
<!-- <param-value>/WEB-INF/spring/appServlet/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-mapping>
</web-app>
这表示 root-context.xml
应该在 /WEB-INF/spring/
中找到 - 一个不存在的目录,至少看起来不存在(可能是classpath:
可能与它有关?)
值得注意的是,在我进行篡改之前,该项目运行良好(显示“Hello World”消息。
所以我不太确定如何继续。有什么建议吗?
最佳答案
哦,你在源树和 war 树之间混淆了。
Eclipse source tree goes in war at accessible with
src/main/resource/xx /WEB-INF/classes/xx classpath:/xx
src/main/webapp/WEB-INF/yy /WEB-INF/yy /WEB-INF/yy
因为/src/main/webapp
是Web应用程序servlet上下文的根目录,src/main/resource
下的文件与编译后的java类文件一起复制在WEB-INF/classes
因此,您不得将classpath:
用于/WEB-INF
下的任何内容,并且您不得对类路径中的任何内容使用 /src/main/...
。
关于java - 无法打开 ServletContext 资源 [/src/main/resources/security-context.xml],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30149982/