spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)
一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别
insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部,
replace:用fragment(包括fragment的节点tag)替换掉当前节点
include:把fragment的内容(不包括fragment的节点)插入到当前节点内容
文字描述还是不够直观,
看代码的测试更容易了解三者的区别
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/fragment
2,功能说明:
演示了thymeleaf模板:insert/include/replace三种引用fragment方式的区别
3,项目结构:如图:

三,配置文件说明
1,pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2,application.properties
#error
server.error.include-stacktrace=always
#error
logging.level.org.springframework.web=trace #thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
四,java代码说明
1,HomeController.java
@RequestMapping("/home")
@Controller
public class HomeController {
@GetMapping("/home")
public String index(ModelMap modelMap) {
return "home/home";
}
}
2,footer.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span style="background: #ffff00;" th:fragment="copytxt">
2013-2019 数据文化传播有限公司版权所有
</span>
</body>
</html>
3,home.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div>
<div id="content" style="width:1040px;">
<div style="width:790px;float:left;margin-left:30px;">
<!--main begin-->
<div style="background: #ff0000;width:400px;height:100px;" th:insert="common/footer :: copytxt"></div>
<div style="background: #00ff00;width:400px;height:100px;" th:replace="common/footer :: copytxt"></div>
<div style="background: #0000ff;width:400px;height:100px;" th:include="common/footer :: copytxt"></div>
<!--main end-->
</div>
</div>
</body>
</html>
分别用三种方式引用footer中的copytxt片断,
比较异同
五,测试效果
1,访问home:
http://127.0.0.1:8080/home/home
如图:

查看源代码比较:
<!--main begin-->
<div style="background: #ff0000;width:400px;height:100px;"><span style="background: #ffff00;">
2013-2019 数据文化传播有限公司版权所有
</span></div>
<span style="background: #ffff00;">
2013-2019 数据文化传播有限公司版权所有
</span>
<div style="background: #0000ff;width:400px;height:100px;">
2013-2019 数据文化传播有限公司版权所有
</div>
<!--main end-->
可以看到三者的区别:
insert:把整个fragment插入到当前节点内部,
replace:用fragment替换到当前节点
include:把fragment的内容(不包括fragment的节点)插入到当前节点
六,查看spring boot版本:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)的更多相关文章
- javascript中var let const三种变量声明方式
javascript中var let const三种变量声明方式 1.var ①var表示声明了一个变量,并且可以同时初始化该变量. ②使用var语句声明的变量的作用域是当前执行位置的上下文:一个函 ...
- Spring学习(十八)Bean 的三种依赖注入方式介绍
依赖注入:让调用类对某一接口实现类的依赖关系由第三方注入,以移除调用类对某一接口实现类的依赖.接下来将详细的向大家介绍Spring容器支持的三种依赖注入的方式以及具体配置方法:• 属性注入方法• ...
- .NET中的三种接口实现方式
摘自:http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html 一般来说.NET提供了三种不同的接口实现方式,分别为隐式接 ...
- spring boot: thymeleaf模板引擎使用
spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...
- eclipse 设置文本模板中 insert variable... 函数 详解
设置文本模板简要图: 设置文本模板详细过程:http://www.cnblogs.com/lsy131479/p/8478711.html 此处引出设置文本模板中 insert variable... ...
- 深入浅出spring IOC中三种依赖注入方式
深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...
- C语言中最常用的三种输入输出函数scanf()、printf()、getchar()和putchar()
本文给大家介绍C语言中最常用的三种输入输出函数scanf().printf().getchar()和putchar(). 一.scanf()函数格式化输入函数scanf()的功能是从键盘上输入数据,该 ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- ASP.NET缓存中Cache过期的三种策略
原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...
随机推荐
- Typecho 使用
安装 下载Typecho 链接:typecho 下载后得到一个压缩文件,解压后获得目录如下: 将该文件夹改名为blog并且上传到/var/www/blog目录下:如下: 修改apache配置 参考:链 ...
- 深入理解Java之装箱与拆箱
一.Java数据类型 1.在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是: 整数型:byte̵,short̵,int̵,l ...
- unserialize3 攻防世界
序列化是将对象转换为便于保存的字符串, 而反序列化是将便于保存的字符串转换为字符串. _wakeup()魔法方法 如果直接传参给code会被__wakeup()函数再次序列化,所以要绕过他, 利用__ ...
- [程序员代码面试指南]数组和矩阵-数组的partition调整
题目 补充问题:数组只含0,1,2,对数组排序,要求时间复杂度O(n),额外空间复杂度O(1) 题解 维护三个变量,l,idx,r.左区间[0,l],中间区间[l+1,idx],右区间[idx+1,r ...
- CRM、用户管理权限
CRM目录结构 from django.shortcuts import HttpResponse,render,redirect from django.conf.urls import url f ...
- Docker数据卷和数据卷容器
是什么 数据卷设计的目的,在于数据的永久化,他完全独立于容器的生存周期,因此,Docker不会在容器删除时删除其挂载的数据卷,也不会存在类似的垃圾收集机制对容器引用的数据卷进行处理.类似我们Redis ...
- 2020 CiGA Game Jam活动总结
CiGA Game Jam 总结 今年8月14.15.16号,48小时游戏开发--Game Jam开始了.蠢新第一次参加Game Jam,今年还是线上开展,情绪复杂= = 还有一个坏消息,晓航旅游缺席 ...
- 【Java并发编程】面试常考的ThreadLocal,超详细源码学习
目录 ThreadLocal是啥?用来干啥? ThreadLocal的简单使用 ThreadLocal的实现思路? ThreadLocal常见方法源码分析 ThreadLocal.set(T valu ...
- Laver 文件版本遍历器
系统简介 最近有个需求,需要罗列出各个目录中文件的信息,检索各类文件的最新版本.网上看了很多方式,但发现没有合适的.于是利用空余时间开始编写了一套文件遍历系统,如此便有了Laver(紫菜).Laver ...
- 03 Comments in C Programming C编程中的注释
Comments 注释简介 Let's take a quick break from programming and talk about comments. Comments help progr ...