Thymeleaf layout 布局

作者: pu_debug | 来源:发表于2016-10-15 11:30 被阅读12409次

第一篇环境搭建好之后,下面接着开始对页面进行布局。通常包含header、footer、menu等等,下面用Thymeleaf来实现,顺便把bootstrap也加进来。另外Thymeleaf版本之间有差异,需要注意。

  • 在templates下新建common文件夹
  • 在common下建一个top_header.html文件
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<header>
<!-- 来自bootstrap例子 -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Project name</a>
    </div>
    <!-- 其他已删 -->
</nav>
</header>
</html>
  • index.html中引入top_header的内容(这样就可以了)
<body>
    <!-- 这种写法,如果top_header中有多个header tag,则都引入进来 -->
    <header th:replace="common/top_header :: header"></header>
</body>

Thymeleaf有多种方式引入

  • th:insert 3.0+版本新加的
  • th:replace 2.0+ 3.0+都可用
  • th:include 这个在3.0版本已经不建议使用

前两个的区别(直接看例子,来自官方):

<!-- 有这么一个html段 -->
<footer th:fragment="copy"> 
  © 2011 The Good Thymes Virtual Grocery
</footer>

<!-- 引用html段 -->
<body>
  <div th:insert="footer :: copy"></div>
  <div th:replace="footer :: copy"></div>
  <div th:include="footer :: copy"></div>
</body>

<!-- 最终结果 -->
<body>
  <!-- th:insert,div tag内部插入html段 -->
  <div>
    <footer>
      © 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>
  <!-- th:replace,使用html段直接替换 div tag -->
  <footer>
    © 2011 The Good Thymes Virtual Grocery
  </footer>
  <!-- th:include,div tag内部插入html段(仅保留段子元素的内容) -->
  <!-- 仔细对比 th:insert 与 th:include的结果 -->
  <div>
    © 2011 The Good Thymes Virtual Grocery
  </div>
</body>

Thymeleaf 布局常用写法:

  • 使用th:fragment给html段指定一个名称,然后通过名称引用,最常用
<!-- 1、通过th:fragment指定一个html段 -->
<footer th:fragment="copy"> 
  © 2011 The Good Thymes Virtual Grocery
</footer>
<!-- 2、通过th:insert或th:replace引用html段 -->
<!-- :: 前面是html页面的相对路径,不需要写后缀(.html);后面是段的名称,即th:fragment="copy" 中的copy -->
<div th:insert="footer :: copy"></div>
  • 直接使用html tag引入,不需要写th:fragment(如最开始的例子)
    <!-- 这种写法,如果top_header中有多个header tag,则都引入进来,适用只有一个相同tag的情况 -->
    <header th:replace="common/top_header :: header"></header>
  • 使用html tag的id属性,不需要写th:fragment
<!-- 一个html段,div指定一个id -->
<div id="copy-section">
  © 2011 The Good Thymes Virtual Grocery
</div>
<!-- 引用上面的html段 -->
<!-- :: 前面是html名称相对路径,不需要写后缀(.html);后面是html tag的id,id前需要加 #,这是3.0+的写法 -->
<div th:insert="~{footer :: #copy-section}"></div>
<!-- 2.0+要这么写 -->
<div th:insert="footer :: #copy-section"></div>

开发时还有一种情况,比如html的head部分,很多公用的东西,但有个别不一样的,如title属性,本页面的css、js之类的,在传统jsp开发时,用<jsp:include>标签可直接引入公用部分,而对于thymeleaf的insert和replace都不好用,后来尝试直接引入页面的方法居然可以了:

<head>
  <title>自己页面的名字<title>

  <!-- 引入整个页面的内容,而不是上面提到的方法(只引入指定的段) -->
  <!-- 官方未提到该用法,这个属于偷巧的做法 -->
  <head th:replace="common/head"/>  <!-- 未使用 :: 符号 -->

  <!-- 下面加自己的css、js,如果有 -->
</head>

看官方文档,这种情况应该使用layout的高级特性:Parameterizable fragment signatures。
高级特性有时间再看一下。


代码参考

相关文章

网友评论

  • 626cf2f51951:楼主,<div layout:fragment="form">和<div th:fragment="approvalForm">有什么区别?
    pu_debug:没用过layout:fragment,不太清楚。后期没再用Themyleaf了....
  • 花绽放水流年:结尾的方式我试过了。确实解决问题,但是它引入了整个html,破坏了引用页的html结构了。
    liuhao_77:<head>
    <title>The awesome application</title>
    <common-head th:replace="common/common-head :: common-head"></common-head>
    </head>

    <head th:fragment="common-head" th:remove="tag"></head>
    pu_debug:结尾的方式倒是不会破坏结构。公用的东西放到一个html里,别加什么标签,引入进来结构是好好的。只是这种方式不好,所以在文章后面写了该使用参数化布局,之后后来不搞这玩意了,也没再研究 T_T
  • 忧郁的小码仔:楼主,如果head部分由公共的一些css文件和每个页面自己的css文件组成,该怎么导入啊。比如我用th:include:

    <head th:include="common/htmlHead::head">
    <link href="css/index.css" rel="stylesheet">
    </head>

    最后生成页面之后,index.css并没有导入进来,楼主有什么解决方法吗?
    忧郁的小码仔:@花绽放水流年 参照官网的写法就可以了:
    <head th:fragment="common_header(title,links)">
    <title th:replace="${title}">The awesome application</title>
    <th:block th:replace="${links}" />
    </head>
    这样就可以通过links参数把每个页面的css全部导入进来。
    花绽放水流年:可以使用带参数的方式:
    include.html
    <head th:fragment="head(title,bizJs)">
    <title th:text="${title}"></title>
    。。。z这里写公共部分
    <script type="text/javascript" th:src="${bizJs}"></script>
    </head>
    子页面中:

    <head th:replace="include::head('后台管理',@{/js/admin.js})"></head>
    pu_debug:文章最后专门写了这种需求的解决

本文标题:Thymeleaf layout 布局

本文链接:https://www.haomeiwen.com/subject/xuzzettx.html