简体   繁体   中英

Could not open ServletContext resource [/src/main/resources/security-context.xml]

I'm trying to study and modify an example project. I started by trying to 'merge' it of sorts with an already existing Google OAuth project, then the above error comes up.

I'm a complete noob to Spring, but clearly this is telling me that it can not find what it is supposed to (in this case, security-context.xml).

Seems a little unorthodox (online examples usually put it in /WEB-INF/Spring), but then... here's the directory structure (in Spring Tool Suite):

在此处输入图片说明

And here's my 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>

Which says that root-context.xml should be found at /WEB-INF/spring/ - a directory that does not exist, at least visibly (could the classpath: might have something to do with it?)

Of note is that prior to my tampering, this project ran fine (displays a "Hello World" message.

So I'm not really sure how to proceed. Any suggestions?

Oups, you make some confusion between the source tree and the war tree.

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

Because /src/main/webapp is the root of the web application servlet context, and files under src/main/resource are copied along with compiled java class files under WEB-INF/classes

So you shall not use classpath: for anything under /WEB-INF , and you shall not use /src/main/... for anything in classpath.

You're confusing the layout of the source project, and the layout of the deployed webapp.

WEB-INF is not part of the classpath. So the path classpath:WEB-INF/spring/root-context.xml is incorrect.

Everything under src/main/resources in the source project ends up under /WEB-INF/classes in the deployed war file, and /WEB-INF/classes is one of the roots of the classpath.

I'm not sure why you put your spring xml files in different locations, but if you want to keep it the way it is, then the path for the root context must be /WEB-INF/spring/root-context.xml , and the path for security-context.xml must be classpath:security-context.xml .

Note that you also made the same mistake for the servlet-context.xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM