thymeleaf模板 在html中引入另一个html

博客介绍了Thymeleaf模板在HTML中引入另一个HTML的方法。以head.html和welcome.html为例,说明了引用页面时的注意事项,如“include=head”中head为引用页面名,“::”符号前后要有空格。还介绍了th:include和th:replace的不同引入效果。

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

thymeleaf模板 在html中引入另一个html

1.这是一个 head.html 页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<div th:fragment="headname">
我是头部这里...
</div>
</body>
</html>

主要代码:

<div th:fragment="headname">
这里面写内容....
</div>

2.welcome.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--引入头部head.html-->
<div th:include="head :: headname"></div>
</body>
</html>

其中注意一点:这里的“include=head” 其中head为你引用的页面的名字**

<div th:include="head :: headname"></div>

要注意一点: “::” 这个符号前后要有空格才可以

还有一个也可以引入:

th:replace 也可以引入模块

th:include:引入子模块的children,依然保留父模块的tag。
加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容。
th:replace:引入子模块的所有,不保留父模块的tag。
替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值