1. ~{templatename::selector}id选择器的引用
2.~{templatename::fragmentname}模板名引用
footer.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<body>
<div th:fragment="copy" id="copy1">
<h1>我是被引用的公共片段</h1>
</div>
</body>
</html>
这里命名一个公共的模板片段叫 copy
<body>
...
<!--至这里引用一个-->
<div th:insert="~{footer :: copy}"></div>
</body>
如果使用模板名,根据前后缀的配置来进行解析 footer 是一个html的界面 copy 是用th:fragment取名的模板名 使用模板名这个写成
<body>
...
<!--至这里引用一个-->
<div th:insert="footer :: copy"></div>
<!--省略~{ }-->
</body>
还有一种使用id选择器的引用
<body>
...
<!--至这里引用一个,属性引用-->
<div th:insert="~{footer :: copy1}"></div>
</body>
三种引用的方式
th:insert 将公共片段整个插入到声明引用标签中
th:replace 将声明引用的标签替换成引入的公共标签
th:include 将引入的标签内的内容引入到声明引用标签中
例子:
<body>
...
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
</body
result
<body>
...
<!--th:insert-->
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<!--th:replace-->
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<!--th:include-->
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
</body>