一.抽取公共片段
1)、方式一:使用th:fragment="片段名"
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
2)、方式二:给片段取class或者id名
<div class="copy" id="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
二.引入公共片段
1)、th:insert、 th:replace、th:include引入方式对比
例如: th:insert="~{templatename::selector}
参数说明:
th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中(去掉片段最外层元素)
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名
三种引入效果对比:
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
效果
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
2)、注意
如果使用th:insert等属性进行引入,可以不用写~{};
行内写法必须加上~{}:[[~{}]]、[(~{})];