Thymeleaf教程 (八) 模板布局(thymeleaf的主要技术优点)

这节主要介绍模板的引入。及如何在不改变前端人员的html显示结果的状况下设计模板(经过属性配置动态时不显示的部分)。css

模板模块导入

首先定义一个/WEBINF/templates/footer.html文件: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 th:fragment="copy">
            &copy; 2011 The Good Thymes Virtual Grocery
        </div>
    </body>
</html>

上面的代码定义了一个片断称为copy,咱们能够很容易地使用th:include 或者 th:replace属性包含在咱们的主页上:前端

<body>
...
<div th:include="footer :: copy"></div>
</body>

include的表达式想当简洁。这里有三种写法:后端

  • “templatename::domselector” 或者 “templatename::[domselector]”引入模板页面中的某个模块。
  • “templatename”引入模板页面。
  • “::domselector” 或者 “this::domselector” 引入自身模板的模块 
    上面全部的templatename和domselector的写法都支持表达式写法:
<div th:include="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>

不使用th:fragment来引用模块

...
<div id="copy-section">
&copy; 2011 The Good Thymes Virtual Grocery
</div>
...

咱们能够用css的选择器写法来引入less

<body>
...
<div th:include="footer :: #copy-section"></div>
</body>

th:include 和 th:replace的区别

th:include和th:replace均可以引入模块,二者的区别在于 
th:include:引入子模块的children,依然保留父模块的tag。 
th:replace:引入子模块的全部,不保留父模块的tag。 
举个栗子:dom

<footer th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

引入界面:this

<body>
...
<div th:include="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
</body>

结果是:spa

<body>
...
<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
</body>

给引入模块添加参数

咱们的模块当中确定有须要有参数的需求:设计

<div th:fragment="frag (onevar,twovar)">
    <p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>

好比在文本中显示参数能够这样中:code

<div th:include="::frag (${value1},${value2})">...</div>
<div th:include="::frag (onevar=${value1},twovar=${value2})">...</div>

第二种用法中参数顺序并不重要:

<div th:include="::frag (twovar=${value2},onevar=${value1})">...</div>

引入没有被定义的模块参数

这段模块没有定义参数

<div th:fragment="frag">
...
</div>

咱们能够而且只能用第二种方式引入:

<div th:include="::frag (onevar=${value1},twovar=${value2})">

这个也等同于:

<div th:include="::frag" th:with="onevar=${value1},twovar=${value2}">

解析式删除不须要的内容(这才是此技术最吸引人的地方,可让前端和后端使用同一个模板,而且都能看到本身想要的效果)

通常状况下后端处理后的界面是这样的:

<table>
    <tr>
        <th>NAME</th>
        <th>PRICE</th>
        <th>IN STOCK</th>
        <th>COMMENTS</th>
    </tr>
    <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
        <td>
            <span th:text="${#lists.size(prod.comments)}">2</span> comment/s
            <a href="comments.html"
            th:href="@{/product/comments(prodId=${prod.id})}"
            th:unless="${#lists.isEmpty(prod.comments)}">view</a>
        </td>
    </tr>
</table>

这只是个模板文件,不是前端写好的预览文件,那么要和前端写好的预览文件一至,咱们通常状况下只能增长虚拟的行.

<table>
    <tr>
        <th>NAME</th>
        <th>PRICE</th>
        <th>IN STOCK</th>
        <th>COMMENTS</th>
    </tr>
    <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
        <td>
            <span th:text="${#lists.size(prod.comments)}">2</span> comment/s
            <a href="comments.html"
            th:href="@{/product/comments(prodId=${prod.id})}"
            th:unless="${#lists.isEmpty(prod.comments)}">view</a>
        </td>
    </tr>
    <tr class="odd">
        <td>Blue Lettuce</td>
        <td>9.55</td>
        <td>no</td>
        <td>
        <span>0</span> comment/s
    </td>
    </tr>
    <tr>
        <td>Mild Cinnamon</td>
        <td>1.99</td>
        <td>yes</td>
        <td>
        <span>3</span> comment/s
        <a href="comments.html">view</a>
        </td>
    </tr>
</table

OK.如今咱们有三行了。看起来和前端的预览文件一致了。那么咱们经过thymeleaf处理后的结果确定是正确的内容+虚拟的内容,其实咱们要的只是正确的内容而已。 
为了解决这个问题th:remove华丽登场了。

<table>
    <tr>
        <th>NAME</th>
        <th>PRICE</th>
        <th>IN STOCK</th>
        <th>COMMENTS</th>
    </tr>
    <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
        <td>
        <span th:text="${#lists.size(prod.comments)}">2</span> comment/s
        <a href="comments.html"
        th:href="@{/product/comments(prodId=${prod.id})}"
        th:unless="${#lists.isEmpty(prod.comments)}">view</a>
        </td>
    </tr>
    <tr class="odd" th:remove="all">
        <td>Blue Lettuce</td>
        <td>9.55</td>
        <td>no</td>
        <td>
        <span>0</span> comment/s
        </td>
    </tr>
    <tr th:remove="all">
        <td>Mild Cinnamon</td>
        <td>1.99</td>
        <td>yes</td>
        <td>
        <span>3</span> comment/s
        <a href="comments.html">view</a>
        </td>
    </tr>
</table>

这个模板在后端开发经过thymeleaf解析后会移除掉有th:remove的标签,知足后端的预期。同时在前端眼中,也是本身预览的效果。

th:remove总共有五种属性:

  • all : 移除tag标记和children。
  • body:保留tag标记和移除children。
  • tag :移除tag和保留children.
  • all-but-first :保留tag和移除除了第一个外的全部children。
  • none :什么都不作。 
    如下是all-but-first的栗子:
<table>
    <thead>
        <tr>
            <th>NAME</th>
            <th>PRICE</th>
            <th>IN STOCK</th>
            <th>COMMENTS</th>
        </tr>
    </thead>
    <tbody th:remove="all-but-first">
        <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
            <td th:text="${prod.name}">Onions</td>
            <td th:text="${prod.price}">2.41</td>
            <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
            <td>
            <span th:text="${#lists.size(prod.comments)}">2</span> comment/s
            <a href="comments.html"
            th:href="@{/product/comments(prodId=${prod.id})}"
            th:unless="${#lists.isEmpty(prod.comments)}">view</a>
            </td>
        </tr>
        <tr class="odd">
            <td>Blue Lettuce</td>
            <td>9.55</td>
            <td>no</td>
            <td>
            <span>0</span> comment/s
            </td>
        </tr>
        <tr>
            <td>Mild Cinnamon</td>
            <td>1.99</td>
            <td>yes</td>
            <td>
            <span>3</span> comment/s
            <a href="comments.html">view</a>
            </td>
        </tr>
    </tbody>
</table>

固然属性也支持表达式:

<a href="/something" th:remove="${condition}? tag : none">Link text not to be removed</a>
<a href="/something" th:remove="${condition}? tag">Link text not to be removed</a>
相关文章
相关标签/搜索