thymeleaf的fragment例子
fragment介绍
fragment类似于JSP的tag,在html中文件中,可以将多个地方出现的元素块用fragment包起来使用。
定义fragment
新建foot.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="footDiv">
the content of footer
</footer>
</html>
fragment的引用
- th:insert:保留自己的主标签,保留th:fragment的主标签。
- th:replace:不要自己的主标签,保留th:fragment的主标签。
- th:include:保留自己的主标签,不要th:fragment的主标签。(官方3.0后不推荐)
新建test.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<!--导入片段-->
<div th:insert="footer :: footDiv"></div>
<div th:replace="footer :: footDiv"></div>
<div th:include="footer :: footDiv"></div>
</html>
得到的结果为
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<!--导入片段-->
<div><footer>
the content of footer
</footer></div>
<footer>
the content of footer
</footer>
<div>
the content of footer
</div>
</html
fragment的参数设置
定义:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<div th:fragment="headDiv(showInfo)">
the content of head!message:[(${showInfo})]
</div>
</html>
调用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<!--导入片段-->
<div th:include="head :: headDiv('测试')"></div>
</html>
结果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<!--导入片段-->
<div>
the content of head!message:测试
</div>
</html>
如果是 多个参数的时候例子:
<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div> 按参数定义时的顺序进行传递
<div th:replace="::frag (${value1},${value2})">...</div>
<div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>
可以不按照参数定义的顺序
<div th:replace="::frag (twovar=${value2},onevar=${value1})">...</div>
fragment的lexible layouts
定义:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head(title,links,scripts)">
<title th:replace="${title}">The awesome application</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/static/css/test.css" rel="stylesheet">
<script type="text/javascript" src="/static/js/jquery.js"></script>
<th:block th:replace="${links}" />
<th:block th:replace="${scripts}" />
</head>
</html>
使用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="head :: head(~{::title},~{::link},~{::script})">
<title>html的title</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
<script th:src="@{/js/bootstrap.js}"></script>
</head>
</html>
结果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>html的title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/static/css/test.css" rel="stylesheet">
<script type="text/javascript" src="/static/js/jquery.js"></script>
<link rel="stylesheet" href="/css/bootstrap.css">
<script src="/js/bootstrap.js"></script>
</head>
</html>
注意是link 和script,不是links 和scripts
如果调用的页面没有link或者script ,则指定传入的参数为~{}即可。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="head :: head(~{::title},~{::link},~{})">
<title>html的title</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
</head>
</html>
thymeleaf的fragment例子的更多相关文章
- spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)
一,thymeleaf如何给fragment传递参数? 1,如果是全局的参数,可以用interceptor中传递 非全局参数,可以从controller中传递 2,引用片断时也可以传递参数 说明:刘宏 ...
- thymeleaf之fragment
MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...
- Thymeleaf+Spring整合
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- thymeleaf 专题
Thymeleaf 之 内置对象.定义变量.URL参数及标签自定义属性 如标题所述,这篇文章主要讲述Thymeleaf中的内置对象(list解析.日期格式化.数字格式化等).定义变量.获取URL的参数 ...
- spring+thymeleaf实现表单验证数据双向绑定
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- Thymeleaf模板引擎+Spring整合使用方式的介绍
尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...
- SPRING + THYMELEAF 配置
1.使用的是Spring EL而不是Ognl.2.访问上下文的Bean用${@myBean.doSomething()}3.th:field,th:errors,th:errorclass用于form ...
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
- Android之Fragment(碎片)方方面面
Fragment简介碎片(Fragment)是一种可以嵌入到活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间. Fragment的生命周期 它与Activity生命周期的关系: 可以看到 ...
随机推荐
- centos7.4 调整根目录大小
由于系统初始时根目录一般空间为50左右 有时会被占满,而/home目录却有大量空间 这里需要调整: 1.终止占用 /home 进程 fuser -m -v -i -k /home 2.备份/home ...
- bind啊你返回的函数到底是个虾米
一般apply().call()和bind()会一起比较. 他们三个都是改变this对象指向的方法. apply()和cal()方法是会立即执行,apply把参数作为数组,call方法接收一个一个的参 ...
- Good Time 冲刺 二
第二天 日期:2018.6.15 一.今日完成任务情况及遇到的问题 王怡镔: 今天学习了小程序框架和组件方面的知识,在微信开发工具中尝试进行小程序开发,学习视图层与逻辑层的框架与联系. 于鑫宇: 学习 ...
- Linux下MySQL在知道密码的情况下修改密码
1.在知道原密码的情况下,进入MySQL: mysql -u root -p 2.进入 mysql数据库,然后通过语句修改密码: 我的MySQL版本是:mysql Ver 14.14 Distrib ...
- 在android studio写car的app代码时遇到的问题
1 Cannot resolve symbol '@drawable/XXX'等问题解决办法方法1."Build " -> "Clean project" ...
- Lesson Learned
最近,中兴ZTE违反美国商务部禁令,向伊朗出售敏感技术,被美国下达长达7年的禁止令,教训十分深刻.以诚待人,信守承诺,才能在商业社会站稳脚跟. 还是说说最近自己上的一课吧.上了港台服以后,奇奇怪怪的问 ...
- POJ - 1830:开关问题 (开关问题-高斯消元-自由元)
pro:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开. ...
- Python:从入门到实践--第八章-函数-练习
#.消息:编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么. #调用这个函数,确认显示的消息无误 def display_message(name): pr ...
- python基础(七)
一.接口开发 import pymysql def my_db(sql): conn = pymysql.connect( host='118.24.3.40', user='jxz', passwo ...
- 第四次Scrum冲刺----Life in CCSU
一.第四次Scrum任务 小组GitHub地址链接 个人GitHub地址链接:https://github.com/2505486985/FirstScrum 继续上次完成的任务,这次完成校园服务中的 ...