SpringBoot学习笔记【part13】Thymeleaf抽取公共页面

SpringBoot 学习笔记 Part13

1. 用th:fragment属性或id封装

第一种方式是使用 th:fragment属性 来进行公共页面的封装,代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <body>
  
    <div th:fragment="copy">
      copy by th:fragment.
    </div>
  
  </body>
</html>

第二种方式是使用 id 属性 来进行公共页面的封装,代码如下:

<div id="copy-section">
  copy by id.
</div>

注:这两种方法都需要借助一个 div 标签来实现公共代码块的封装。


2. 用th:insert、th:replace、th:include抽取

在需要加入公共页面的地方使用属性 th:insert、th:replace、th:include 完成公共页面的抽取。

这三个属性的value值需使用 ~{…}表达式,也可以省去。

使用 th:fragment 封装的抽取:

<body>

  ...

  <div th:insert="~{footer :: copy}"></div>
  <div th:insert="footer :: copy"></div>
  
</body>

使用 id 封装的抽取:

<body>

  ...

  <div th:insert="~{footer :: #copy-section}"></div>
  <div th:insert="footer :: #copy-section"></div>
  
</body>

th:insert、th:replace、th:include 三者的区别:

<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
  1. th:insert 会直接插入到使用 th:insert 的那个标签的标签体里去,最外层标签(div)会还在。

    <div>
        <footer>
            copy success.
        </footer>
    </div>
    
  2. th:replace 会直接把使用 th:replace 的那个标签整个给换了,即干干净净地放入公共页面。

    <footer>
        copy success.
    </footer>
    
  3. th:include 会把公共页面使用 th:fragment 的标签给拿掉,也就是本页面最外层标签还会在,但是公共部分的最外层被除去了。

    <div>
        copy success.
    </div>
    

3. 案例

第一步:案例代码准备。

head标签的样式部分:

<head>
    <meta charset="UTF-8">
    <title>Title</title>

  <link href="css/style.css" rel="stylesheet" th:href="@{/css/style.css}">
  <link href="css/style-responsive.css" rel="stylesheet" th:href="@{css/style-responsive.css}">

  <script src="js/html5shiv.js" th:src="@{/js/html5shiv.js}"></script>
  <script src="js/respond.min.js" th:src="@{/js/respond.min.js}"></script>

</head>

body标签体底部的script部分:

<body>
    ...
    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/jquery-ui-1.9.2.custom.min.js"></script>
    <script src="js/jquery-migrate-1.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/modernizr.min.js"></script>
    <script src="js/jquery.nicescroll.js"></script>

    <script src="js/scripts.js"></script>
</body>

第二步:新建一个common.html,meta和title标签不要,并为css和js引用标签添加 thymeleaf 属性。

head标签的样式部分:(新建一个th属性这种方式能在无服务端渲染时打开)

<head>

    <link href="css/style.css" rel="stylesheet" th:href="@{/css/style.css}">
    <link href="css/style-responsive.css" rel="stylesheet" th:href="@{css/style-responsive.css}">

    <script src="js/html5shiv.js" th:src="@{/js/html5shiv.js}"></script>
    <script src="js/respond.min.js" th:src="@{/js/respond.min.js}"></script>
        
</head>

body标签体底部的script部分:

<body>
    ...
    <script th:src="@{/js/jquery-1.10.2.min.js}"></script>
    <script th:src="@{/js/jquery-ui-1.9.2.custom.min.js}"></script>
    <script th:src="@{/js/jquery-migrate-1.2.1.min.js}"></script>
    <script th:src="@{/js/bootstrap.min.js}"></script>
    <script th:src="@{/js/modernizr.min.js}"></script>
    <script th:src="@{/js/jquery.nicescroll.js}"></script>

    <script th:src="@{/js/scripts.js}"></script>
</body>

第三步:使用th:fragment属性或id封装。

head标签的样式部分:(直接用head标签里即可)

<head th:fragment:"commonheader">

    <link href="css/style.css" rel="stylesheet" th:href="@{/css/style.css}">
    <link href="css/style-responsive.css" rel="stylesheet" th:href="@{css/style-responsive.css}">

    <script src="js/html5shiv.js" th:src="@{/js/html5shiv.js}"></script>
    <script src="js/respond.min.js" th:src="@{/js/respond.min.js}"></script>
        
</head>

body标签体底部的script部分:(body还有放别的公共部分,所以另起一个div)

<body>
    ...
    <div th:fragment="commonscript">
        <script th:src="@{/js/jquery-1.10.2.min.js}"></script>
        <script th:src="@{/js/jquery-ui-1.9.2.custom.min.js}"></script>
        <script th:src="@{/js/jquery-migrate-1.2.1.min.js}"></script>
        <script th:src="@{/js/bootstrap.min.js}"></script>
        <script th:src="@{/js/modernizr.min.js}"></script>
        <script th:src="@{/js/jquery.nicescroll.js}"></script>

        <script th:src="@{/js/scripts.js}"></script>
    </div>
</body>

第四步:在需要使用公共页面的html里抽取即可。

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

   <div th:replace="common :: commonheader"></div>

</head>
<body>
    
    ...
    
	<div th:replace="common :: #commonscript"></div>
    
</body>
</html>

没时间学技术了,2月份了,要准备考研初试了,写给自己。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Parker7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值