springboot开发之thymeleaf页面公共元素的抽取

时间:2022-07-23
本文章向大家介绍springboot开发之thymeleaf页面公共元素的抽取,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、抽取公共元素

<footer th:fragment="copy">
    context
</footer>

2、引入公共片段

(1)第一种方式:th:insert,表示将公共片段整个插入到指定元素中,比如div

<div th:insert="~{footer:: copy}"></div>

说明:

~{templatename::selector}:模板名::选择器

~{templatename::fragmentname}:模板名::片段名

(2)第二种方式:th:replace,将声明的片段直接替换指定元素中,比如div。也就是说这种不会带<div></div>

(2)第三种方式:th:include,将声明片段的内容直接插入到指定元素中。也就是说这种不会包含<footer></footer>