最近做到页面数据展示分页的功能,由于每个模块都需要分页,所以每个页面都需要将分页的页码选择内容重复的写N遍,如下所示:


重复的代码带来的就是Ctrl+C,Ctrl+V ,于是了解了一下thymeleaf的fragment加载语法以及th:include、th:replace的区别,得以解决。

首先在pom.xml引入thymeleaf的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

将上述的重复信息抽取出来存为pagination.html

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><div class="panelBar" th:fragment="pagination"><!--以下为公共部分--><div class="pages"><span>显示</span><select class="combox" name="numPerPage" οnchange="navTabPageBreak({numPerPage:this.value})"><option value="1" th:selected="${pages.numPerPage}==1">1</option><option value="3" th:selected="${pages.numPerPage}==3">3</option><option value="5" th:selected="${pages.numPerPage}==5">5</option><option value="10" th:selected="${pages.numPerPage}==10">10</option><option value="100" th:selected="${pages.numPerPage}==100">100</option><option value="150" th:selected="${pages.numPerPage}==150">150</option><option value="200" th:selected="${pages.numPerPage}==200">200</option><option value="250" th:selected="${pages.numPerPage}==250">250</option></select><span id="fleeceRecordCounts" th:text="'共有'+${pages.totalCount}+'条'"></span></div><div id="fleece_page" class="pagination"  th:attr="targetType=${pages.targetType},totalCount=${pages.totalCount},numPerPage=${pages.numPerPage},pageNumShown=${pages.pageNumShown},currentPage=${pages.currentPage}"></div></div></body>
</html>

在其他页面进行引用该公共模块时如下:

<div class="panelBar" th:replace="pagination::pagination"></div>

注意:第一个pagination为上述公共部分的文件名,第二个pagination为th:fragment的值。这样便可以解决公共部分代码的抽取。

fragment加载语法如下:

templatename::selector:”::”前面是模板文件名,后面是选择器
::selector:只写选择器,这里指fragment名称,则加载本页面对应的fragment
templatename:只写模板文件名,则加载整个页面

  ================== fragment语法 ============================= <!--  语法说明  "::"前面是模板文件名,后面是选择器 --><div th:include="template/footer::copy"></div><!-- 只写选择器,这里指fragment名称,则加载本页面对应的fragment --><div th:include="::#thispage"></div><!-- 只写模板文件名,则加载整个页面 --><div th:include="template/footer"></div>
================= 加载块 ============================<span id="thispage">div in this page.
</span>

th:include 和 th:replace都是加载代码块内容,但是还是有所不同

th:include:加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容
th:replace:替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div
公共部分如下:

<!-- th:fragment 定义用于加载的块 -->
<span th:fragment="pagination">
the public pagination
</span>

引用时如下:

================= th:include 和 th:replace============================
<!-- 加载模板的内容: 读取加载节点的内容(不含节点名称),替换<div>的内容 -->
<div th:include="pagination::pagination">1</div>
<!-- 替换当前标签为模板中的标签: 加载的节点会整个替换掉加载他的<div>  -->
<div th:replace="pagination::pagination">2</div>

结果如下:

<!-- 加载模板的内容: 读取加载节点的内容(不含节点名称),替换<div>的内容 -->
<div> the public pagination</div>
<!-- 替换当前标签为模板中的标签: 加载的节点会整个替换掉加载他的<div>  -->
<span> the public pagination</span>

Thymeleaf th:include、th:replace使用相关推荐

  1. Thymeleaf th:include、th:replace使用

    最近做到页面数据展示分页的功能,由于每个模块都需要分页,所以每个页面都需要将分页的页码选择内容重复的写N遍,如下所示: 重复的代码带来的就是Ctrl+C,Ctrl+V ,于是了解了一下thymelea ...

  2. thymeleaf 语法——th:text默认值、字符串连接、th:attr、th:href 传参、th:include传参、th:inline 内联、th:each循环、th:with、th:if

    文章目录 1.表达式 1.1.简单表达式 1.2.三元运算.条件表达式: 1.3.默认表达式 1.4.综合使用,属性值的 if - else 2.字符串连接.拼接 3.th:attr 的使用 4.th ...

  3. th:replace th:include

    原文:https://blog.csdn.net/h1021456873/article/details/79085505 Thymeleaf html 导入(th:replace & th: ...

  4. Thymeleaf提取公共页面(从实例入手,以inspinia模板为例)

    场景 使用Thymelaf 时,可以提取公共页面,比如侧边栏.需要公共引入的css.js文件.页脚等都可以提取出来.只编写业务页面的内容部分即可. 三种不同引入方式的区别 th:insert:将代码块 ...

  5. thymeleaf的属性优先级

    所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签.这个顺序是: Order Feature Attributes 1 Fragment inclusion th:include t ...

  6. Thymeleaf框架

    简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在 ...

  7. SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎

    SpringBoot 学习笔记 Part12 1. thymeleaf简介 SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染. Thymeleaf is a modern ...

  8. SpringBoot2.x 集成 Thymeleaf

    本文主要对SpringBoot2.x集成Thymeleaf及其常用语法进行简单总结,其中SpringBoot使用的2.4.5版本. 一.Thymeleaf简介 Thymeleaf是面向Web和独立环境 ...

  9. Thymeleaf模板(全程案例详解)

    Thymeleaf 1.thymeleaf简介 2.thymeleaf的使用 3.i18n国际化 4.thymeleaf变量获取 5.url变量 6.字面量获取和设置 7.迭代元素 8.条件判断的使用 ...

最新文章

  1. 最好的程序界面就是用户无需去阅读操作手册就知道该如何使用的界面
  2. C语言 —— 预定义符号__FILE____LINE___func__(编写调试函数printf , debug)
  3. 中国银行业100强发布,头部名企为何选择永洪科技?
  4. 服务器开发中网络数据分析与故障排查经验
  5. 你的微博也被盗赞?试试HSTS强制HTTPS加密
  6. Linux云服务器安装Redis并设置远程连接设置开机自启
  7. 开发工具:IDEA 调试技巧,非常实用,欢迎收藏!
  8. 关于xfce中桌面没法显示回收站以及thunar中无法进行卷管理的解决办法
  9. c/c++标准库中的文件操作总结
  10. c语言入门敲打,C语言基础三(敲打键盘、寻找资料,循环语句)
  11. 引用类型-Function类型
  12. oracle 自增加列的实现
  13. vue2.0 实现页面导航提示引导
  14. 中药图片拍照识别系统全套开源
  15. 5-3 凸多边形面积
  16. {2018.4.12}荀(gou)彧(huo)同学的第一次考试小总结
  17. 部分STL_hanhan~
  18. 利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, double value):将第row行第col列的元素赋值为valu
  19. 【Aegisub相关】VSCode插件:Aegisub Helper的安装、功能介绍及使用方法
  20. 活动库存限制是“递增”还是“递减”、你认为哪种方式更好些???

热门文章

  1. 如何使用oprofile对软件做profiling
  2. test libvirt
  3. c语言管程例子,管程 - it610.com
  4. idea pom依赖失败_IDEA中Maven依赖下载失败的完美解决方案
  5. 岗位理解_当面试官问求职者对应聘岗位的理解情况
  6. python流行趋势_Python流行度再创新高,学Python就从风变编程开始
  7. c语言回文字符串原理,回文串(c语言)注意字符串比较和字符比较的区别
  8. 2021-04-08
  9. python关闭浏览器删除session_Django操作session 的方法
  10. Delphi及C++Builder经典图书一览表(持续更新中2018.01.02)