fragment介绍

fragment类似于JSP的tag,在html中文件中,可以将多个地方出现的元素块用fragment包起来使用。

定义fragment

新建foot.html文件

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <footer th:fragment="footDiv">
  5. the content of footer
  6. </footer>
  7. </html>

fragment的引用

  1. th:insert:保留自己的主标签,保留th:fragment的主标签。
  2. th:replace:不要自己的主标签,保留th:fragment的主标签。
  3. th:include:保留自己的主标签,不要th:fragment的主标签。(官方3.0后不推荐)

新建test.html文件

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <!--导入片段-->
  5. <div th:insert="footer :: footDiv"></div>
  6. <div th:replace="footer :: footDiv"></div>
  7. <div th:include="footer :: footDiv"></div>
  8. </html>

得到的结果为

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <!--导入片段-->
  5. <div><footer>
  6. the content of footer
  7. </footer></div>
  8. <footer>
  9. the content of footer
  10. </footer>
  11. <div>
  12. the content of footer
  13. </div>
  14. </html

fragment的参数设置

定义:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <div th:fragment="headDiv(showInfo)">
  5. the content of head!message:[(${showInfo})]
  6. </div>
  7. </html>

调用:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <!--导入片段-->
  5. <div th:include="head :: headDiv('测试')"></div>
  6. </html>

结果:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <!--导入片段-->
  5. <div>
  6. the content of head!message:测试
  7. </div>
  8. </html>

如果是 多个参数的时候例子:

  1. <div th:fragment="frag (onevar,twovar)">
  2. <p th:text="${onevar} + ' - ' + ${twovar}">...</p>
  3. </div>
  4.  
  5. 按参数定义时的顺序进行传递
  6. <div th:replace="::frag (${value1},${value2})">...</div>
  7. <div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>
  8. 可以不按照参数定义的顺序
  9. <div th:replace="::frag (twovar=${value2},onevar=${value1})">...</div>

fragment的lexible layouts

定义:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <head th:fragment="head(title,links,scripts)">
  5. <title th:replace="${title}">The awesome application</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <link href="/static/css/test.css" rel="stylesheet">
  8. <script type="text/javascript" src="/static/js/jquery.js"></script>
  9. <th:block th:replace="${links}" />
  10. <th:block th:replace="${scripts}" />
  11. </head>
  12. </html>

使用:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <head th:include="head :: head(~{::title},~{::link},~{::script})">
  5. <title>html的title</title>
  6. <link rel="stylesheet" th:href="@{/css/bootstrap.css}">
  7. <script th:src="@{/js/bootstrap.js}"></script>
  8. </head>
  9. </html>

结果:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>html的title</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <link href="/static/css/test.css" rel="stylesheet">
  8. <script type="text/javascript" src="/static/js/jquery.js"></script>
  9. <link rel="stylesheet" href="/css/bootstrap.css">
  10. <script src="/js/bootstrap.js"></script>
  11. </head>
  12. </html>

注意是link 和script,不是links 和scripts 
如果调用的页面没有link或者script ,则指定传入的参数为~{}即可。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <head th:include="head :: head(~{::title},~{::link},~{})">
  5. <title>html的title</title>
  6. <link rel="stylesheet" th:href="@{/css/bootstrap.css}">
  7. </head>
  8. </html>

thymeleaf的fragment例子的更多相关文章

  1. spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)

    一,thymeleaf如何给fragment传递参数? 1,如果是全局的参数,可以用interceptor中传递 非全局参数,可以从controller中传递 2,引用片断时也可以传递参数 说明:刘宏 ...

  2. thymeleaf之fragment

    MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...

  3. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  4. thymeleaf 专题

    Thymeleaf 之 内置对象.定义变量.URL参数及标签自定义属性 如标题所述,这篇文章主要讲述Thymeleaf中的内置对象(list解析.日期格式化.数字格式化等).定义变量.获取URL的参数 ...

  5. spring+thymeleaf实现表单验证数据双向绑定

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  6. Thymeleaf模板引擎+Spring整合使用方式的介绍

    尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...

  7. SPRING + THYMELEAF 配置

    1.使用的是Spring EL而不是Ognl.2.访问上下文的Bean用${@myBean.doSomething()}3.th:field,th:errors,th:errorclass用于form ...

  8. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  9. Android之Fragment(碎片)方方面面

    Fragment简介碎片(Fragment)是一种可以嵌入到活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间. Fragment的生命周期 它与Activity生命周期的关系: 可以看到 ...

随机推荐

  1. render函数(转)

    https://blog.csdn.net/qq78827534/article/details/80792514

  2. SQL Server DDL

    1:向表中添加字段 Alter table [表名] add [列名] 类型 2:  删除字段 Alter table [表名]  drop column [列名] 3:  修改表中字段类型 (可以修 ...

  3. github仓库主页介绍、用git管理本地仓库和github仓库、搭建网站

    github仓库主页介绍 名词解释: 工作区: 添加.编辑.修改文件等动作 暂存区: 暂存已经修改的文件,最后统一提交到git中 git(仓库): 最终确定的文件保存到仓库,成为一个新的版本,并且对他 ...

  4. swagger2常用注解

    常用注解: @Api()用于类: 表示标识这个类是swagger的资源 @ApiOperation()用于方法: 表示一个http请求的操作 @ApiParam()用于方法,参数,字段说明: 表示对参 ...

  5. 关于QQ屏蔽某些文件上传一些有意思的事

    起因是因为某鱼事件... 资源倒是找了,可是因为TX的屏蔽文件上传.文件通过几次改名之后也没能上传成功(想想也没那么简单). 然后某神就写了上面那串代码实现上传.具体思想是:绕过文件摘要算法(hash ...

  6. redis数据库可视化工具(RedisDesktopManager)

    RedisDesktopManager下载地址:https://redisdesktop.com/download 使用过程中可能会遇到的问题我在文后有所总结 我下载Windows版的: 我的redi ...

  7. Sublime Text 3 package control 解决 "There are no packages available for installation" 错误

    造成这个错误的原因估计是因为由于packagecontrol.io无法访问,所以导致无法读取“https://packagecontrol.io/channel_v3.json”文件的原因. 解决方法 ...

  8. EmptyBeanUtil

    package com.rscode.credits.util; import java.util.List; /** * * 判断实体是否为空 * @author tn * */ public cl ...

  9. hbuilder中的wap2app (将M站快速转换成App的开发框架)使用过程有关原生标题的关闭

    首先,我最近在做有关将M站快速转换成App的项目,在网上看了很多,最终结合同学的推荐,我选择了hbuilder,有关于hbuilder的下载还有具体使用方法,官网都有详细的说明,我就不介绍了,我重点介 ...

  10. springboot+mybatis-puls利用swagger构建api文档

    项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...