简述
一般来说,一个网站的各个网页中的导航栏,页尾,菜单等部分都是相同或相似的,可以将这部分代码提取成模板供其它页面使用。
要实现该功能,可以使用Thymeleaf模板的 th:replace、th:include或th:insert属性,借助th:fragment属性来实现。
属性 | 特点 |
---|---|
th:replace | 不保留自己的主标签,保留th:fragment的主标签 |
th:include | 保留自己的主标签,不保留th:fragment的主标签 |
th:insert | 保留自己的主标签,也保留th:fragment的主标签 |
th:include标签时 未改变;
而其他两者导航栏均发生改变
用法如下:
给公共的片段中加入th:fragment属性。
<footer th:fragment="copy">
这里标记属性值为“copy”
公共元素(顶部栏/侧边栏)