如何正确引用 CSS 路径?

How to reference the CSS path properly?

我在一个 Java/Spring 项目中工作,下面提供了项目结构,

我需要在 index.jsp 文件中引用 app.css。目前,我在 index.jsp 中引用了以下方式,应用程序没有获取 CSS 信息。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Bitcoin Wallet</title>
    <link href="${pageContext.request.contextPath}/resources/css/app.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
</html>

dispatcher-servlet.xml中,我提供的资源路径如下,

我在localhost中得到的页面显然没有css。如何解决问题?

要在 spring mvc 项目中使用资源,您必须声明 mvc:resources,将 url 映射到物理文件路径位置。

<mvc:resources mapping="/resources/**" location="/resources/css/"

当您在 jsp 文件中使用 /resources/ 时,spring 会将此 url 映射到 /resources/css/

接下来可以在jsp文件中引用css文件:

<link href="<c:url value="/resources/css/app.css" />" rel="stylesheet">

并将 resources 文件夹移动到 webapp 文件夹中