th:include、th:replace、th:insert区别

首先先在pom中引入thymeleaf的依赖

        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

yml的配置为

spring:
  #模板引擎
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    cache: false

默认使用html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<footer th:fragment="copy" id="1">
    this is footer
</footer>

<!--在本标签内引入-->
<div th:insert="::copy"></div>
<!--全部引入,替换掉本标签内容-->
<div th:replace="::copy"></div>
<!--引入时用本标签替换掉要引用的模块标签-->
<div th:include="::copy"></div>


结果为
<!--在本标签内引入-->
<div>
    <footer id="1">
        this is footer
    </footer>
</div>
<!--全部引入,替换掉本标签内容-->
<footer id="1">
    this is footer
</footer>
<!--引入时用本标签替换掉要引用的模块标签-->
<div>
    this is footer
</div>


<!--怎么使用?-->
<!--templatename::selector:”::”前面是模板文件名,后面是选择器;选择器是自己定义的名称,例如<footer th:fragment="copy" id="1"></foot>-->
<!--::selector:只写选择器,这里指fragment名称,则加载本页面对应的fragment-->
<!--templatename:只写模板文件名,则加载整个页面-->

<!--引入common文件夹下pagination.html文件中的test1选择器-->
<div th:replace="common/pagination::test1"></div>
<!--引入本文件中的名为copy选择器-->
<div th:replace="::copy"></div>
<!--引入整个文件,文件在common文件夹下的common.html-->
<div th:include="common/common"></div>

总结:
    可以根据自己的需要使用insert/include/replace,来引入本文件,其它文件的选择器,或页面
    来进行代码的复用
</body>
</html>

文章转载自https://blog.csdn.net/austral/article/details/73802396

https://blog.csdn.net/believe__sss/article/details/79992408

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值