thymeleaf的layout常用的有两种方式用法
第一种
将页面里的每个部分都分成 块 -> fragment 使用 th:include 和 th:replace 来引入页面
这种用法没有layout的概念, 因为每个部分都是 fragment, 下面例子说明
<!-- index.html -->
<html>
<head>
<meta charset="utf-8"/>
<title>demo</title>
</head>
<body>
<div th:include="components/header :: header"></div>
<div class="container">
<h1>hello world</h1>
</div>
<div th:include="components/footer :: footer"></div>
</body>
</html> <!-- components/header.html -->
<header th:fragment="header">
<ul>
<li>news</li>
<li>blog</li>
<li>post</li>
</ul>
</header>
<!-- components/footer.html -->
<header th:fragment="footer">
<div>i am footer.</div>
</header>
上面例子里用到的是th:include, 也就是把定义好的fragment引入的意思, 还有一个是th:replace, 意思是替换当前页面里的这部分代码, 下面例子说明一下
<html>
<head>
<meta charset="utf-8"/>
<title>demo</title>
</head>
<body>
<div th:replace="components/header :: header">
<!-- 使用th:replace进来的header.html会替换下面的这个header -->
<header>
<ul>
<li>static - news</li>
<li>static - blog</li>
<li>static - post</li>
</ul>
</header>
</div>
<div class="container">
<h1>hello world</h1>
</div>
<div th:include="components/footer :: footer"></div>
</body>
</html>
第二种
写一个layout.html页面,当作页面的基础页面
<!-- layout/layout.html -->
<html>
<head>
<meta charset="utf-8"/>
<title>demo</title>
</head>
<body>
<div th:include="components/header :: header"></div>
<div layout:fragment="content"></div>
<div th:include="components/footer :: footer"></div>
</body>
</html>
在子页面里使用 layout:decorator 来将子页面里的内容加入到 layout.html里去
<!-- index.html -->
<html layout:decorator="layout/layout">
<head>...</head>
<body>
<div layout:fragment="content">
<h2>hello world!!!</h2>
</div>
</body>
</html>
这样在layout.html里引入的css,js,imgs都可以在子页面里用了,而且在子页面里还可以引入子页面需要用到的css,js,imgs, 就很方便了 推荐
模板传值,假如要往header.html里传入一个tab来区别应该高亮哪个菜单,可以使用下面的写法实现, 先定一个样式
 .active {background-color: green;}
<header th:fragment="header (tab)">
<ul>
<li>
<span th:class="${tab eq 'news'} ? active">news</span>
</li>
<li>
<span th:class="${tab eq 'blog'} ? active">blog</span>
</li>
<li>
<span th:class="${tab eq 'post'} ? active">post</span>
</li>
</ul>
</header>
调用写法
<div th:include="components/header :: header(tab='blog')"></div>

thymeleaf layout的更多相关文章

  1. 解决thymeleaf layout布局不生效

    今天使用thymeleaf layout布局时总是不生效,特此把解决问题的步骤和几个关键点记录下来备忘. 一.检查依赖 1.thymeleaf必备maven依赖: <dependency> ...

  2. 关于thymeleaf+layout布局的使用方式,spring boot 访问页面(静态页面及jsp页面)

    首先建立相同部分的html,这里我命名为layout.html,放在了`templates/layout'文件夹下,这个路径以后是会用到的,以下是我的layout的代码,比较粗糙. 但是应该会更好的帮 ...

  3. 一篇好文档,请Thymeleaf Layout Dialect

    Thymeleaf Layout Dialect https://ultraq.github.io/thymeleaf-layout-dialect/ This will introduce the  ...

  4. Thymeleaf利用layout.html文件生成页面布局框架

    1.layout.html文件 生成布局 <!DOCTYPE html> <html lang="zh-CN" xmlns:th="http://www ...

  5. thymeleaf 布局layout

    以前写过一篇使用thymeleaf实现div中加载html 大部分内容都没问题,只是部分知识已经过时了. 重新记录: 依赖依然是 <dependency> <groupId>n ...

  6. thymeleaf 专题

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

  7. springboot~thymeleaf页面布局的步骤

    参考:https://ultraq.github.io/thymeleaf-layout-dialect/Installation.html 依赖包 注意里面的thymeleaf-layout-dia ...

  8. springboot+mybatis+thymeleaf项目搭建及前后端交互

    前言 spring boot简化了spring的开发, 开发人员在开发过程中省去了大量的配置, 方便开发人员后期维护. 使用spring boot可以快速的开发出restful风格微服务架构. 本文将 ...

  9. Spring-boot(二)--thymeleaf

    @Controller @RequestMapping("/") public class MessageController { private final MessageRep ...

随机推荐

  1. Jsoup教程,jsoup开发指南,jsoup中文使用手册,jsoup中文文档

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. jsou ...

  2. [Python] json 报错'xxx is not JSON serializable'的处理方法

    predictions = self.model.predict(x_data, verbose=0)[0] y_pred_idx = np.argmax(predictions) y_pred_pr ...

  3. Visual Studio Community 版本过期

    /********************************************************************* * Visual Studio Community 版本过 ...

  4. Vim:基础

    Normal模式 即是command “vim test.txt” 时进入的界面. 常用command: :help<Enter>   查看命令. :wq<Enter>     ...

  5. BZOJ1131 POI2008 Sta 【树形DP】

    BZOJ1131 POI2008 Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=10 ...

  6. 20179223《Linux内核原理与分析》第三周学习笔记

    测试3的实验: 1. 用gcc -g编译vi输入的代码 2. 在main函数中设置一个行断点 3. 在main函数增加一个空循环,循环次数为自己学号后4位,设置一个约为学号一半的条件断点 4. 提交调 ...

  7. jenkins for mac

    Mac环境中Jenkins的停止和启动命令启动sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist停止sudo launchc ...

  8. SQL Server 为存储过程添加预定设置注释代码

    一个优秀的项目最少不了的是代码注释,兴许你是代码高手入目既知道该段代码主要功能是什么,但日子长了,记的东西多了,即时再熟悉的代码也渐渐的有点不认 识它,所以养成良好的写注释的习惯,对于自己对于他人都是 ...

  9. 《DSP using MATLAB》示例Example7.8

    窗函数的设计思想就是选择一个理想的频率选择滤波器(通常其脉冲响应函数是 非因果.无限长的),然后截断(取窗)这个无限长脉冲响应,得到一个线性相位.因果的 FIR滤波器.频率域示意图如下: 我们的目的: ...

  10. BZOJ4057 [Cerc2012]Kingdoms

    题意 有一些王国陷入了一系列的经济危机.在很多年以前,他们私底下互相借了许多钱.现在,随着他们的负债被揭发,王国的崩溃不可避免地发生了--现在有n个王国,对于每对王国A和B,A欠B的钱被记为d_AB( ...