0

我的我的项目文件夹目录如下

SpringMVCTest/WebContent/WebInf/jsp/index.jsp

css 文件位于

SpringMVCTest/WebContent/style/main.css

我的问题是我无法从 index.jsp 访问 css 文件。我试过了

<link rel="stylesheet" type="text/css" href="../style/main.css">

和其他人。

请有任何建议。

4

1 回答 1

1

您是否将以下内容添加到您的 servlet-context.xml 文件中?
编辑
您可以在下面的 Web.xml 文件中找到该文件的读取位置:至少这是 Spring Mvc 3.1 中的位置,这是我的项目使用的.

<!-- 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>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

您的 servlet-context.xml 文件:

<!-- Handles HTTP GET requests for /WebContent/** by efficiently serving up static resources in the ${webappRoot}/WebContent directory -->
<resources mapping="/WebContent/**" location="/WebContent/" />

然后从你的jsp你应该能够使用:

<link rel="stylesheet" type="text/css" href="/WebContent/style/main.css">

上面告诉spring不要通过DispatcherServlet处理这些静态资产

于 2012-12-31T10:24:18.333 回答