SpringBoot----thymeleaf的公共部分的抽取31

把页面的一些通用的部分单独拿出来,其他页面来引用,就如JSP里面的include

第一步:声明公共片段

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
    <body> 
        <div th:fragment="copy"> 
            &copy; 2011 The Good Thymes Virtual Grocery 
        </div> 
    </body> 
</html>

第二步:引入公共片段

片段名

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

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

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

注意:这里的模板名的意思是你片段所在html文件的文件名,而选择器是指公共片段的ID名。如下

选择器(id选择器)

<div id="copy-section"> 
    &copy; 2011 The Good Thymes Virtual Grocery 
</div>

<body>
 ... 
    <div th:insert="~{footer :: #copy-section}">
    </div> 
</body>

其他的引入方式

<body> ... 
    <div th:insert="footer :: copy"></div> 
    <div th:replace="footer :: copy"></div> 
    <div th:include="footer :: copy"></div> 
</body>

insert:将片段插入到指定元素中

replace:将声明的元素替换到为指定的公共元素中

th:include:将被引入的片段的内容包含进这个标签中


insert的公共片段在div标签中

如果使用th:insert等属性进行引入,可以不用写~{}

行内写法可以加上:[[~{}]];[(~{})]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值