thymeleaf 包含处理

本文介绍Thymeleaf中页面包含的两种方式:th:replace和th:include,并演示了如何通过th:with传递参数到被包含页面,适用于需要高效页面布局的Web开发者。

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


在所有项目中页面的互相包含是一项非常重要的技术支持,而在thymeleaf也同样支持有数据的包含处理,而对于包含操作在thymeleaf模板之中提供有两种支付语法
th:replace; 是使用标签进行替换 原始的宿主标签还在 但是包含标签不在;
th:include; 是进行包含 原始的宿主标签消失 而保留包含的标签

1.0 既然要定义被包含的页面 于是建立“src/main/resources/templates/commons/footer”页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Title</title>
</head>
<footer th:fragment="companyInfo">
    <p style="color: red">zw(程序生活)</p>
</footer>
</html>

2.0 随后要进行页面的包含处理

<div th:replace="@{/commons/footer}::companyInfo"></div>
<br/>
<div th:include="@{/commons/footer}::companyInfo"></div>

3.0 在很多的开发之中都需要向被包含页面进行参数的传递 在thymeleaf 之中也可以实现一样的处理操作形式 使用 th:with的处理模式完成

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Title</title>
</head>
<footer th:fragment="companyInfo">
    <p style="color: red">zw(程序生活)</p>


    <!--/*@thymesVar id="id" type="com"*/-->
    <p th:text="${id}"/>、<p th:text="${suid}"></p>
    <!--/*@thymesVar id="suid" type="com"*/-->
</footer>
</html>

4.0 而后在进行包含处理的时候可以按照如下的方式进行参数的传递

<div th:include="@{/commons/footer}::companyInfo" th:with="id=3,suid=35"></div>

githua  代码如下   https://github.com/zhouwei520/SpringbootDemo/tree/master/demo9  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

伟子涵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值