30.在springboot中使用thymeleaf的模板(include,insert)

本文介绍了如何在Spring Boot项目中使用Thymeleaf插件实现模板的插入和包含,通过实例展示了`th:insert`和`th:include`的不同用法,以及如何引用和复用head和footer等公共模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明

本项目是在

https://blog.csdn.net/weixin_59334478/article/details/126770911?spm=1001.2014.3001.5501icon-default.png?t=M85Bhttps://blog.csdn.net/weixin_59334478/article/details/126770911?spm=1001.2014.3001.5501springboot文章29

修改而来

pom.xml文件,核心配置文件等文件,与文章29保持一致,本文只展示最新添加的文件,以减小文章冗余,更容易看出thymeleaf的使用方法。

知识点:

1.插入模板相当于,在当前标签中间插入引用过来的模板:例如

<div th:insert="~{head :: top}">
    第一种方式使用模板,th:insert
</div>

就等于

<div th:insert="~{head :: top}">
<div th:fragment="top">
    <p>百度公司</p>
    <p>网站:www.baidu.com</p>
</div>
</div>

2.包含模板就相当于,引用过来的模板替换了当前的标签,例如:

<div th:include="~{head :: top}">
</div>

就等于

<div th:fragment="top">
    <p>百度公司</p>
    <p>网站:www.baidu.com</p>
</div>

项目结构:

 

1.index.html添加连接语句

 

2.ThymeleafController类添加方法

 

3.创建html文件

1)head.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>head</title>
</head>
<body>
          <div th:fragment="top">
              <p>百度公司</p>
              <p>网站:www.baidu.com</p>
          </div>


        <div th:fragment="menu">
            <p>文档|下载|blog</p>

        </div>
</body>
</html>

2)footer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>footer</title>
</head>
<body>
       <div>
           footer.html
           @www.baidu.com
       </div>
</body>
</html>

3)common目录下的leaf.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    左侧树形结构
    部门管理
    人员管理
    考勤入口
</div>

</body>
</html>

4)customtpl.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>使用模板</title>
</head>
<body>
      <div style="margin-left: 200px;">

          <h3>使用其他目录中的模板文件</h3>
          <div th:insert="common/left :: html"></div>
          <br/>
          <br/>
          <div th:include="common/left"></div>


          <div th:insert="head :: menu"></div>


          <br/>
          <br/>
          <h3>插入模板  th:insert    第一种方式</h3>
          <div th:insert="~{head :: top}">
              第一种方式使用模板,th:insert
          </div>

          <br/>
          <br/>
          <h3>插入模板   th:insert   第二种方式</h3>
          <p th:insert="head :: top"></p>


          <br/>
          <br/>
          <h3>包含模板,  th:include   第一种语法</h3>
          <div th:include="~{head :: top}">

          </div>


          <br/>
          <br/>
          <h3>包含模板,  th:include   第二种语法</h3>
          <div th:include="head :: top">

          </div>


          <br/>
          <br/>
          <h3>使用整个文件作为复用的内容(整个文件作为模板使用)</h3>
          <div th:include="footer :: html"></div>
          <br/>
          <br/>
          <div th:include="footer"></div>
          <br/>
          <br/>
          <div th:insert="footer"></div>




      </div>
</body>
</html>

4.测试

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值