Thymeleaf引入公共片段

1.公共片段

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>公共片段</title>
</head>
<body>
    <!--声明代码片段-->
   <div th:fragment="header_comment">
       <p>
           这是声明公共片段
       </p>
   </div>
    <!--通过选择器声明-->
    <div id="header_comment_id">
        <p>
            这是id声明公共片段
        </p>
    </div>
</body>
</html>

 2.引入公共片段

<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>模板文件</title>
</head>
<body>
  <h2>成功</h2>
  <hr/>
   <!--th:text 设置p标签的标签体的内容-->
   <p th:text="${name}">显示名字</p>

  <!--引入公共代码片段
      通过th:fragment声明的
      注意:header 是文件名
            header_comment 是声明的属性值
      -->
   <div th:replace="header :: header_comment"></div>

  <!--通过id选择器进行声明代码片段-->
  <!--注意:header 是文件名-->
  <!--header_comment_id是声明的那个id-->
   <div th:replace="header :: #header_comment_id" class="aaa"></div>

   <!--th:insert 和 th:replace的区别-->
      <!--th:insert和th:replace都可以引入片段,两者的区别在于-->
      <!--th:insert-》保留引入时使用的标签 (整个插入过来)-->
      <!--th:replace-》不保留引入时使用的标签,将声明片段直接覆盖当前引用标签(整个拿过来进行替换)-->
   <h2 th:insert="header :: #header_comment_id" class="bbb"></h2>
</body>
</html>

关键-》th:fragmen声明片段   th:insert 和th:replace引入片段

 th:insert和th:replace区别

有两种方式声明引入

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值