我的意思不是taglibs,而是使用JSP标签执行以下操作:
ChildPage.jsp:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:layout>
<jsp:attribute name="head">
<link href="css/custom.css" type="text/css" rel="stylesheet"/>
</jsp:attribute>
<jsp:attribute name="scripts">
<script src="js/custom.js"></script>
</jsp:attribute>
<jsp:body>
<p>This is from the child page</p>
</jsp:body>
</t:layout>
layout.tag:
<%@ tag description="Layout template" pageEncoding="UTF-8" %>
<%@ attribute name="head" fragment="true" %>
<%@ attribute name="scripts" fragment="true" %>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/main.css" type="text/css" rel="stylesheet"/>
<jsp:invoke fragment="head"/>
</head>
<body>
<div id="body">
<p>This is from the parent or "layout"</p>
<jsp:doBody/>
</div>
<div id="footer">
<script src="js/main.js"></script>
<jsp:invoke fragment="scripts"/>
</div>
</body>
</html>
呈现时:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/main.css" type="text/css" rel="stylesheet"/>
<link href="css/custom.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="body">
<p>This is from the parent or "layout"</p>
<p>This is from the child page</p>
</div>
<div id="footer">
<script src="js/main.js"></script>
<script src="js/custom.js"></script>
</div>
</body>
</html>
这使我可以在布局页面和子页面中的JSP的标头部分中包含脚本。正文和页脚相同。
我已经阅读了Thymeleaf的文档/示例,但也许我不太了解,因为它看起来好像我无法做到。
我之所以“反转”似乎是一个简单的include的原因是,我拥有的每个页面都包含某些脚本和页眉节,但是我的子页面也具有要导入的脚本和要包含的样式表。
我能以某种方式实现吗?我做错了吗?
默认情况下,Thymeleaf使用所谓的包含样式布局。这种方法的缺点在官方网站上有解释。我建议您使用Thymeleaf布局方言。这是创建Hierarchical-style布局的更为方便的方言。
顺便说一句,在“布局方言”中,<head>
标记的所有内容将自动合并。只是看一个例子。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句