系统中的很多页面有很多公共内容,例如菜单、页脚等,这些公共内容可以提取放在一个称为“模板片断”的公共页面里面,其它页面可以引用这个
“模板片断”内容。

一、模板片断的定义

可以是html标签,也可以使用th:fragment属性定义片断。

二、引用片断

1、使用th:insert属性插入片断,除此之外,还可以使用th:replace和th:include插入。
语法:
(1) th:insert="~{模板名称}"
插入模板的整个内容

(2) th:insert="~{模板名称::选择器}"
插入模板的指定内容,选择器可以对应th:fragment定义的名称,也可以用类似JQuery选择器的语法选择部分片断。
片断选择器语法:
a) /name,选择子节点中节点名称为name的节点
b) //name,选择全部子节点中节点名称为name的节点
c) name[@attr='value'] 选择名称为name并且属性值为value的节点,如有多个属性可用and连接
d) //name[@attr='value'][index] 选择名称为name并且属性值为value的节点,指定节点索引
片断选择器的简化语法:
a) 可以省略 @ 符号
b) 使用 # 符号代替 id 选择,如div#id等价于div[id='id']
c) 使用 . 符号代替 class 选择,如div.class等于于div[class='class']
d) 使用 % 代替片断引用,如片断节点使用了th:ref或th:fragment,则可使用div%ref来选取节点

(3) th:insert="~{::选择器}"
不指定模板名称,则选择器作用于当前页面

(4) th:insert="~{this::选择器}"
与"~{::选择器}"类似,不同之处是在本页面找不到片断时,会到模板引擎的process方法处理的模板中寻找片断。

2、th:insert、th:replace、th:include的区别
th:insert 当前标签里面插入模板中的标签
th:replace替换当前标签为模板中的标签
th:include前标签里面插入模板的标签内容

3、模板片断也支持传入变量
引用语法:~{footer.html::名称(参数)

4、片断块引用
可以使用th:block定义片断块,th:block是一个属性容器,可以在里面添加任何的th属性。
例如表格的循环体中一般在tr中用th:each,也可以用th:block改写。

5、删除模板
使用th:remove删除模板,属性值:
all:删除当前节点,包括子节点
body:删除当前节点的全部子节点
tag:删除当前节点,不包括子节点
all-but-first:除了当前节点下面的第一个子节点,其它全部删除
none:不进行任何操作

三、使用实例

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml
加入Thymeleaf依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、src/main/java/com/example/demo/TestController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class TestController {
@RequestMapping("/")
public String test(){
return "test";
}
}

3、src/main/resources/templates/footer.html

<span th:fragment="frag1">frag1</span>
<span th:fragment="frag2">frag2</span> <div id="footer1">footer1</div> <div>
<div id="footer2">footer2</div>
</div> <div>
<span class="content">footer3</span>
<span class="content">footer4</span>
</div> <div th:fragment="welcome(userName)">
<span th:text="|hello,| + ${userName}"></span>
</div>

4、src/main/resources/templates/test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h4>th:insert引用片断</h4>
引用指定模板的整个内容
<div th:insert="~{footer.html}"></div>
引用指定模板的片断
<div th:insert="~{footer.html::frag1}"></div>
引用本页面的片断
<div th:insert="~{::frag3}"></div>
<div th:insert="~{this::frag3}"></div>
<div th:fragment="frag3">frag3</div> <h4>th:replace、th:include与th:insert的区别</h4>
<div th:replace="~{footer.html::frag1}"></div>
<div th:include="~{footer.html::frag1}"></div> <h4>片断选择器的部分用法</h4>
<div th:insert="~{footer.html::/div[@id='footer1']}"></div>
<div th:insert="~{footer.html:://div#footer2}"></div>
<div th:insert="~{footer.html::span[class='content']}"></div>
<div th:insert="~{footer.html:://span[class='content'][0]}"></div>
<div th:insert="~{footer.html:://span.content}"></div>
<div th:insert="~{footer.html::span%frag1}"></div> <h4>含有变量的片断引用</h4>
<div th:insert="~{footer.html::welcome('小明')}"></div> <h4>片断块引用</h4>
<table>
<th:block th:each="number : ${#numbers.sequence(0,1)}">
<tr>
<td th:text="${number}"></td>
</tr>
</th:block>
</table> <h4>删除模板</h4>
<table>
<th:block th:each="number : ${#numbers.sequence(0,1)}">
<tr th:remove="${number > 0} ? all : none">
<td th:text="${number}"></td>
</tr>
</th:block>
</table> </body>
</html>

IDEA运行后,浏览器访问:http://localhost:8080,查看网页源代码,结果如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h4>th:insert引用片断</h4>
引用指定模板的整个内容
<div><span>frag1</span>
<span>frag2</span> <div id="footer1">footer1</div> <div>
<div id="footer2">footer2</div>
</div> <div>
<span class="content">footer3</span>
<span class="content">footer4</span>
</div> <div>
<span>hello,null</span>
</div></div>
引用指定模板的片断
<div><span>frag1</span></div>
引用本页面的片断
<div><div>frag3</div></div>
<div><div>frag3</div></div>
<div>frag3</div> <h4>th:replace、th:include与th:insert的区别</h4>
<span>frag1</span>
<div>frag1</div> <h4>片断选择器的部分用法</h4>
<div><div id="footer1">footer1</div></div>
<div><div id="footer2">footer2</div></div>
<div><span class="content">footer3</span><span class="content">footer4</span></div>
<div><span class="content">footer3</span></div>
<div><span class="content">footer3</span><span class="content">footer4</span></div>
<div><span>frag1</span></div> <h4>含有变量的片断引用</h4>
<div><div>
<span>hello,小明</span>
</div></div> <h4>片断块引用</h4>
<table> <tr>
<td>0</td>
</tr> <tr>
<td>1</td>
</tr> </table> <h4>删除模板</h4>
<table> <tr>
<td>0</td>
</tr> </table> </body>
</html>

Thymeleaf常用语法:模板片断的更多相关文章

  1. 1-9springboot之thymeleaf常用语法(html页面)

    一.引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 在html中引入此命名空间,可避免编辑器出现html验证错误,虽 ...

  2. Thymeleaf常用语法:表达式语法之运算符

    Thymeleaf表达式语法之常量分为字符串常量.数字常量.布尔值常量.空值常量:运算符分为算术运算符.关系运算符.条件运算符.无操作符. 开发环境:IntelliJ IDEA 2019.2.2Spr ...

  3. Thymeleaf常用语法:模板注释

    Thymeleaf模板注释分为标准HTML/XML注释.解析层注释.原型注释三种. 一.注释说明 1.标准HTML/XML注释 直接通过浏览器打开,不显示,Thymeleaf模板引擎解析也不处理,但查 ...

  4. Thymeleaf常用语法:模板文件中表达式调用Java类的静态方法

    在模板文件的表达式中,可以使用“${T(全限定类名).方法名(参数)}”这种格式来调用Java类的静态方法. 开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1. ...

  5. thymeleaf常用语法

    常用标签语法:①th:text<span th:text="${name}">1</span>注释:如果${name}有值则将替换掉1的值,若无则为1 ②t ...

  6. Thymeleaf常用语法:使用星号表达式

    在处理模板时,一般情况都是使用变量表达式 ${...} 来显示变量,还可以使用选定对象表达式 *{...},它也称为星号表达式.如果在模板中先选定了对象,则需要使用星号表达式.Thymeleaf的内置 ...

  7. Thymeleaf常用语法:数据延迟加载

    在处理模板时,可以由模板逻辑决定是否加载数据,以提高性能.在Spring Boot控制器中设置数据时,使用LazyContextVariable可以实现这功能. 开发环境:IntelliJ IDEA ...

  8. Thymeleaf常用语法:自定义数据转换类

    在模板文件中,可以使用“${{...}}”表达式进行数据转换,Thymeleaf会使用配置好的数据转换类,来实现转换.例如一个User对象,简单起见假设有姓名和年龄两个字段,对象的toString() ...

  9. Thymeleaf常用语法:数据迭代

    Thymeleaf数据迭代使用th:each属性,可以迭代数组.List.Set和Map等,数组.List.Set的迭代方法类似,迭代Map则会得到一个java.util.Map.Entry对象.在迭 ...

随机推荐

  1. ABAP基础语法

    1.数据类型及属性 类型 说明 C N 0到9之间字符组成的数字字符串 D 日期格式必须为 YYYYMMDD T 格式为 24-hour的 HHMMSS I -2.147.483.648 to +2. ...

  2. 62-Weave 网络结构分析

    上一节我们安装并创建了 Weave 网络,本节将部署容器并分析网络结构. 在 host1 中运行容器 bbox1: eval $(weave env) docker run --name bbox1 ...

  3. 【重学Node.js 第5篇】部署项目到腾讯云服务器

    课程介绍看这里:https://www.cnblogs.com/zhangran/p/11963616.html 项目github地址:https://github.com/hellozhangran ...

  4. netcore 2.2 封装 AutoMapper

    在上篇中我们通过创建一个类并继承autoMapper的Profile类 public class Mappings : Profile { public Mappings() { CreateMap& ...

  5. SpringBoot 项目运行在 tomcat7 上

    SpringBoot 项目如何打成 war 包 SpringBoot项目的默认打包方式是将工程打包成为一个 jar 包.部分情况下,我们需要将项目打包成一个 war 包,以方便我们将工程部署在 tom ...

  6. 剑指offer笔记面试题8----二叉树的下一个节点

    题目:给定一棵二叉树和其中的一个节点,如何找出中序遍历序列的下一个节点?树中的节点除了有两个分别指向左.右子节点的指针,还有一个指向父节点的指针. 测试用例: 普通二叉树(完全二叉树,不完全二叉树). ...

  7. 【转载】C#中decimal保留2位有效小数

    在C#的数字运算过程中,有时候针对十进制decimal类型的计算需要保留2位有效小数,针对decimal变量保留2位有效小数有多种方法,可以使用Math.Round方法以及ToString先转换为字符 ...

  8. SQL Server如何正确的删除Windows认证用户

    在SQL Server数据库中,有时候会建立一些Windows认证的账号(域账号),例如,我们公司习惯给开发人员和Support同事开通NT账号权限,如果有离职或负责事宜变更的话,那么要如何正确的删除 ...

  9. FTP 代码含义

    vsftpd.config 部分参数含义anonymous_enable=NO #不允许匿名用户登陆 local_enable=YES #vsftpd所在系统的用户可以登录vsftpd write_e ...

  10. cURL无法访问TLS网站故障解决

    大多数人都厌烦使用老旧的系统,无论软件还是硬件.但有的时候又不得不困守其中,坚持延续着系统的寿命,或者还需要点几柱香,祈求神佛的护佑. Linux是一个模块化极好的操作系统,得益于此,当其中有组件落伍 ...