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

一、模板片断的定义

可以是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. Cesium 本地部署案例

    众所周知,cesium的服务器是搭建在国外的,所以我们国内的用户访问的时候贼慢.有时想查个api或者看个案例半天都进不去,今天我来说一下傻瓜式搭建本地的cesium环境,用于大家没事查资料用!步骤:1 ...

  2. ORACLE spool打印

    问题描述:spool让我想起来了spooling假脱机,但是这个spool是oracle下的命令,将select查询出来的数据打印出来 1.linuxi下 spool +路径+文件名,这里的文件如果不 ...

  3. ffmpeg+nginx 实现rtsp转rtmp并通过nginx转发

    Windows安装 ffmpeg ffmpeg windows版下载地址https://ffmpeg.zeranoe.com/builds/ static版本就行 配置环境变量:下载的压缩包解压后的路 ...

  4. Redis学习二(数据操作).

    key 操作 删除 key:del key 批量删除key:redis-cli -a(密码)keys "QXJ_*"| xargs redis-cli -a(密码)del 查看所有 ...

  5. <计算机系统结构中的8个伟大思想>

    摘自<计算机组成与设计>戴维帕森 ——面向摩尔定律的设计 ——使用抽象简化设计 ——加速大概率事件 ——通过并行提高性能 ——通过流水线提高性能 ——存储器层次 ——通过冗余提高可靠性

  6. IT兄弟连 HTML5教程 CSS3属性特效 渐变1

    渐变背景一直以来在Web页面中都是一种常见的视觉元素.但一直以来,Web设计师都是通过图形软件设计这些渐变效果,然后以图片形式或者背景图片的形式运用到页面中.Web页面上实现的效果,仅从页面的视觉效果 ...

  7. centos7 laravel 项目 npm install报错

    npm install 初始化项目依赖的前端资源   报错 ERR xxx .. socket,symbol link is not supported ... 如果报错了 重新npm install ...

  8. springboot使用hibernate validator

    前言 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗长 每次要看哪些参 ...

  9. IS guide:Eric Steven Raymond in《How To Become A Hacker》

    Learn how to program.This, of course, is the fundamental hacking skill. If you don't know any comput ...

  10. August 18th, 2019. Week 34th, Sunday

    Fear doesn't shut you down, it wakes you up. 恐惧不会消磨你的意志,它能激发你的潜能. We all know that fear is powerful, ...