把页面的一些通用的部分单独拿出来,其他页面来引用,就如JSP里面的include
第一步:声明公共片段
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
</html>
第二步:引入公共片段
片段名
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名
注意:这里的模板名的意思是你片段所在html文件的文件名,而选择器是指公共片段的ID名。如下
选择器(id选择器)
<div id="copy-section">
© 2011 The Good Thymes Virtual Grocery
</div>
<body>
...
<div th:insert="~{footer :: #copy-section}">
</div>
</body>
其他的引入方式
<body> ...
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
</body>
insert:将片段插入到指定元素中
replace:将声明的元素替换到为指定的公共元素中
th:include:将被引入的片段的内容包含进这个标签中
insert的公共片段在div标签中
如果使用th:insert等属性进行引入,可以不用写~{}:
行内写法可以加上:[[~{}]];[(~{})];