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. chrome 55 zepto tap事件出错?

    刚才升级chrome后发现的,在54.0.2840.98上点击没有问题,在新升级的55.0.2883.75 上点击后会报错Cannot read property 'trigger' of undef ...

  2. HTTP 1.1 协议规范

    1. 内容协商 请求一个特殊编码的过程在 HTTP 1.1 规范中称为内容协商:

  3. Swift GCD

    var queue: dispatch_queue_t = dispatch_get_main_queue()// 主线程 queue = dispatch_get_global_queue(DISP ...

  4. Android 编程 高德地图 (实现显示地图以及定位功能)

    本文参考文章: http://www.apkbus.com/blog-904057-63610.html 本人实现的 定位代码:(具体配置省略,可见参考文章) package com.example. ...

  5. 如何将局域网中的 windows 硬盘挂载到 linux 系统中

    1.共享windows上的E盘 2.linux上执行 mount //192.168.3.181/e /tmp/test -o username=dell,,password=abcdef 3.保证两 ...

  6. websocket IE11 提示SecurityError

    一开始我从网上下载的示例地上localhost:8081 在其它 浏览器下都正常,就在IE11下提示SecurityError, 然后把我把localhost换成IP,就好了. 依次记录下.

  7. BZOJ3998 TJOI2015 弦论 【后缀自动机】【贪心】

    Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...

  8. Hbase rowkey热点问题

    当处理由连续事件得到的数据时,即时间上连续的数据.这些数据可能来自于某个传感器网络.证券交易或者一个监控系统.它们显著的特点就是rowkey中含有事件发生时间.带来的一个问题便是HBase对于row的 ...

  9. Python学习-lambda表达式

    lambda只是一个表达式,函数体比def简单很多.lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去.lambda表达式是起到一个函数速写的作用.允许在 ...

  10. 《DSP using MATLAB》示例Example 9.5

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...