我使用 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
中,我提供了如下资源路径,
我在本地主机中获得的页面显然没有 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
文件夹中
关于java - 如何正确引用CSS路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643322/