thymeleaf中fragment 的layout布局


1 配置

如果要引入fragment的layout,需要添加依赖:

 
 
 
compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:1.2.1')

2 layout的引用

定义一个layout文件,目录为/WEB-INF/views/task/layout.html,内容如下:

 
 
 
<!DOCTYPE html>
<html>
  <head>
    <!--/*  Each token will be replaced by their respective titles in the resulting page. */-->
    <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
    ...
  </head>
  <body>
    <!--/* Standard layout can be mixed with Layout Dialect */-->
    <div th:replace="fragments/header :: header">
      ...
    </div>
    <div class="container">
      <div layout:fragment="content">
        <!-- ============================================================================ -->
        <!-- This content is only used for static prototyping purposes (natural templates)-->
        <!-- and is therefore entirely optional, as this markup fragment will be included -->
        <!-- from "fragments/header.html" at runtime.                                     -->
        <!-- ============================================================================ -->
        <h1>Static content for prototyping purposes only</h1>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
          Phasellus et placerat elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
          Phasellus et placerat elit.
        </p>
      </div>
      <div th:replace="fragments/footer :: footer">&copy; 2014 The Static Templates</div>
    </div>
  </body>
</html>

上面文件的layout核心配置代码为layout:fragment="content"。在目录WEB-INF/views/task/list.html

下创建内容页面,代码如下:

 
 
 
<!DOCTYPE html>
<html layout:decorator="task/layout">
  <head>
    <title>Task List</title>
    ...
  </head>
  <body>
    <!-- /* Content of this page will be decorated by the elements of layout.html (task/layout) */ -->
    <div layout:fragment="content">
      <table class="table table-bordered table-striped">
        <thead>
          <tr>
            <td>ID</td>
            <td>Title</td>
            <td>Text</td>
            <td>Due to</td>
          </tr>
        </thead>
        <tbody>
          <tr th:if="${tasks.empty}">
            <td colspan="4">No tasks</td>
          </tr>
          <tr th:each="task : ${tasks}">
            <td th:text="${task.id}">1</td>
            <td><a href="view.html" th:href="@{'/' + ${task.id}}" th:text="${task.title}">Title ...</a></td>
            <td th:text="${task.text}">Text ...</td>
            <td th:text="${#calendars.format(task.dueTo)}">July 11, 2012 2:17:16 PM CDT</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>

页面task/list的内容由task/layout的标签配置布局。注意在<html>标签中的属性layout:decorator="task/layout"。该属性会被标记到Layout Dialect,而Layout Dialect就是layout用来给指定视图配置布局用的。

3 在Layout Dialect中用include方式配置布局

下面为复用alert片段的例子,其目录为layout:fragment (task/alert.html),代码如下:

 
 
 
<!DOCTYPE html>
<html>
  <body>
    <th:block layout:fragment="alert">
      <div class="alert alert-dismissable" th:classappend="'alert-' + ${type}">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <h4 th:text="${header}">Alert header</h4>
        <!--/* 'layout:fragment' attribute defines a replaceable content section */-->
        <th:block layout:fragment="alert-content">
          <p>Default alert content</p>
        </th:block>
      </div>
    </th:block>
  </body>
</html>

若要引用上面的片段,可用下面方式:

 
 
 
<div layout:include="task/alert :: alert" th:with="type='info', header='Info'" th:remove="tag">
  <!--/* Implements alert content fragment with simple content */-->
  <th:block layout:fragment="alert-content">
    <p><em>This is a simple list of tasks!</em></p>
  </th:block>
</div>

也可以这样引用:

 
 
 
<div layout:include="task/alert :: alert" th:with="type='danger', header='Oh snap!'" th:remove="tag">
  <!--/* Implements alert content fragment with full-blown HTML content */-->
  <th:block layout:fragment="alert-content">
    <p>Some text</p>
    <p>
      <button type="button" class="btn btn-danger">Take this action</button>
      <button type="button" class="btn btn-default">Or do this</button>
    </p>
  </th:block>
</div>




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值