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

一、模板片断的定义

可以是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. CentOS 7 离线环境安装nginx时报错:./configure: error: C compiler cc is not found

    先说解决方法: 在nginx目录下,查看objs/autoconf.err文件,该文件记录了具体的错误信息 vi objs/autoconf.err 一般就是缺少一些文件,因为我的gcc.g++也是离 ...

  2. QT获取linux下的当前用户名

    故事背景:客户端启动的时候需要加载机器/home/xx/test.jpg的图片作为背景图,但是有的机器用户名叫AAA,有的机器名叫BBB,所以我需要获取当前用户的home目录 技术调研:QStanda ...

  3. Java基础语法04面向对象上-类-属性-方法-可变参数-重载-递归-对象数组

    类 面向对象是一种思想,一般指将事务的属性与方法抽出总结为模板(类/class),处理事务时通过类创建/new出对象由对象的功能/方法去完成所要计算处理的事情. 面向过程:POP:以过程,步骤为主,考 ...

  4. QT在linux下获取网络类型

    开发中遇到这样一个需求,需要判断当前网络的类型(wifi或者4G或者网线),在这里给大家一块分享下: 1.这里有一个linux指令:nmcli(大家自行百度即可) 2.nmcli device sta ...

  5. Redhat Linx使用Centos yum源

    一.故障现象: 在安装了Read linux后,使用yum安装软件,出现如下提示:[root@localhost~]# yum install xxxLoaded plugins: product-i ...

  6. s3c2440裸机-内存控制器(四、SDRAM原理-cpu是如何访问sdram的)

    1.SDRAM原理 black (1)SDRAM内部存储结构: (2)再看看与2440连接的SDRAM原理图: sdram引脚说明: A0-A12:地址总线 D0-D15:数据总线(位宽16,2片级联 ...

  7. centos8 安装 nginx

    http://nginx.org/ NGINX官网 创建文件夹mkdir nginx进入创建的文件夹 根据自己需要下载合适版本 通过 wget http://nginx.org/download/ng ...

  8. Appium常用指令

    右键图片“在新标签页打开”可查看大图

  9. C++ std::forward_list 基本用法

    #include <iostream> #include <string> #include <forward_list> using namespace std; ...

  10. Flink概述| 配置

    流处理技术的演变 在开源世界里,Apache Storm项目是流处理的先锋.Storm提供了低延迟的流处理,但是它为实时性付出了一些代价:很难实现高吞吐,并且其正确性没能达到通常所需的水平,换句话说, ...