在thymeleaf中引入网页碎片

一、文件头引入thymeleaf

thymeleaf文件都要放在springboot的templates目录下

每个文件的头部都要引入网址,注意th不要漏掉

<html lang="en" xmlns:th="http://www.thymeleaf.org">

二、子网页

使用th:fragment标签

th:fragment="fragment1"

完整页面-头部 header.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>header</title>
</head>
<body>
    <div style="background: cadetblue" th:fragment="fragment1">thymeleaf头部</div>
</body>
</html>

完整页面-尾部 footer.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>header</title>
</head>
<body>
    <div th:fragment="fragment2">时间:2021/11/23</div>
</body>
</html>

三、主网页

引入的时候使用th:fragment,属性里要写:子页面名::定义的名字

th:include="header::fragment1"

完整页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div align="center" th:include="header::fragment1"></div>
    <div align="center">
        $$$$$$$$$$$$
        $$$$$$$$$$$$
    </div>
    <div align="center" th:include="footer::fragment2"></div>
</body>
</html>

四、目录关系

五、控制类

templates下面的文件是不能直接访问的,所以要加一个控制器

PathController

@Controller
@RequestMapping("/books")
public class PathController {

    @RequestMapping("/xx.html")
    public String a(){
        return "xx";
    }

}

启动项目后在浏览器上输入 http://www.localhost:8080/books/xx.html

 六、显示效果

内部都能引入,但是子网页的样式没有引入进来,使用的是主网页的样式

七、使用子网页的样式

只需要把 th:include  换成th:replace ,会使用子网页的样式 代替掉主网页的样式

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div align="center" th:replace="header::fragment1"></div>
    <div align="center">
        $$$$$$$$$$$$
        $$$$$$$$$$$$
    </div>
    <div align="center" th:include="footer::fragment2"></div>
</body>
</html>

八、显示效果

子网页的“背景”出来了,主网页的“居中”不见了

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

youngcave2

等待第一笔打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值