我已经访问了之前的相关问题以找到解决方案,但我没有......
How to use "css" in the "jsp" in Spring MVC project?
including style sheet in a jsp page with Spring MVC
我的问题是如何将css文件链接到Spring MVC中的.jsp文件,这个问题被很多人问到了!但不幸的是,这些解决方案无法实现。
文件结构是 - http://i.imgur.com/8M3xpp5.png
请给出指示,谢谢。
答案 0 :(得分:3)
我能够通过以下
实现这一目标 <style type="text/css"><%@ include file="/css/bootstrap.css" %> </style>
其中bootstrap.css是我的css文件,文件夹结构类似。试一试。
答案 1 :(得分:1)
在spring-dispatcher-servlet.xml中添加以下行
<mvc:resources mapping="/css/**" location="/css/" />
希望这会对你有所帮助
答案 2 :(得分:-2)
要在JSP页面中包含JavaScript或CSS等静态资源,您应该执行以下三个步骤:
1)将cs,js或images等静态资源放入此文件夹/ WebContent / resources
2)在bean .xml文件中添加Spring mvc:resources映射
<mvc:resources location="/resources/" mapping="/static/**"></mvc:resources>
<mvc:annotation-driven></mvc:annotation-driven>
3)在JSP页面中包含JS或CSS文件有三种方法:
3.1)JSTL标签c:url:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="<c:url value="/static/css/main.css"/>" rel="stylesheet" type="text/css">
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div id="msg"></div>
</body>
</html>
&#13;
3.2)Spring tag spring:url
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<spring:url value="/static/css/main.css" var="mainCss" />
<link href="${mainCss}" rel="stylesheet" />
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div id="msg"></div>
</body>
</html>
&#13;
3.3)你也可以使用&#34;页面上下文&#34;变量来获取资源
<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main.css" />
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div id="msg"></div>
</body>
</html>
&#13;
快乐的编码。