首页 > 解决方案 > 在 Spring MVC 中将 js/css 导入到 jsp

问题描述

我有一个 Spring MVC 项目,我可以将 css/js 导入我的 index.jsp 但不能用于其他视图。我试过 ${pageContext.request.contextPath} 但它没有用。

我有这个结构。

在此处输入图像描述

小服务程序

resources mapping="/resources/**" location="/resources/theme1/"

意见/*.jsp

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
    <title>Message Page</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

 <script src="<c:url value="/resources/css/messages.js" />"></script>
<link href="<c:url value="/resources/js/messages.css" />" rel="stylesheet">
</head>

在浏览器日志中我得到:

GET http://localhost:8080/ProyectTest/resources/css/messages.js net::ERR_ABORTED 404 - GET http://localhost:8080/ProyectTest/resources/js/messages.css net::ERR_ABORTED 404

标签: springspring-mvc

解决方案


我想我解决了你的问题

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

仔细地看。您的 css 文件在css文件夹内,但您声明了文件夹的路径js。我认为使用这条线对你有用。

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

推荐阅读