Spring Boot(四):Thymeleaf 使用详解

​在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用。Thymeleaf 是新一代的模板引擎,在 Spring4.0 中推荐使用 Thymeleaf 来做前端模版引擎。

Thymeleaf 介绍

简单说,Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:

  • 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 Thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

  • 2.Thymeleaf 开箱即用的特性。它提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL表达式效果,避免每天套模板、该 Jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

  • 3.Thymeleaf 提供 Spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

标准表达式语法

它们分为四类:

  • 1.变量表达式

  • 2.选择或星号表达式

  • 3.文字国际化表达式

  • 4.URL 表达式

变量表达式

变量表达式即 OGNL 表达式或 Spring EL 表达式(在 Spring 术语中也叫 model attributes)。如下所示:
${session.user.name}

它们将以HTML标签的一个属性来表示:

  1. <span th:text="${book.author.name}">

  2. <li th:each="book : ${books}">

选择(星号)表达式

选择表达式很像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如下:
*{customer.name}

被指定的 object 由 th:object 属性定义:

  1. <div th:object="${book}">

  2. ...

  3. <span th:text="*{title}">...</span>

  4. ...

  5. </div>

文字国际化表达式

文字国际化表达式允许我们从一个外部文件获取区域文字信息(.properties),用 Key 索引 Value,还可以提供一组参数(可选).

  1. #{main.title}

  2. #{message.entrycreated(${entryId})}

可以在模板文件中找到这样的表达式代码:

  1. <table>

  2. ...

  3. <th th:text="#{header.address.city}">...</th>

  4. <th th:text="#{header.address.country}">...</th>

  5. ...

  6. </table>

URL 表达式

URL 表达式指的是把一个有用的上下文或回话信息添加到 URL,这个过程经常被叫做 URL 重写。
@{/order/list}

URL还可以设置参数:
@{/order/details(id=${orderId})}

相对路径:
@{../documents/report}

让我们看这些表达式:

  1. <form th:action="@{/createOrder}">

  2. <a href="main.html" th:href="@{/main}">

变量表达式和星号表达有什么区别吗?

如果不考虑上下文的情况下,两者没有区别;星号语法评估在选定对象上表达,而不是整个上下文
什么是选定对象?就是父标签的值,如下:

  1. <div th:object="${session.user}">

  2. <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>

  3. <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>

  4. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>

  5. </div>

这是完全等价于:

  1. <div th:object="${session.user}">

  2. <p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>

  3. <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>

  4. <p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>

  5. </div>

当然,美元符号和星号语法可以混合使用:

  1. <div th:object="${session.user}">

  2. <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>

  3. <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>

  4. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>

  5. </div>

表达式支持的语法

字面(Literals)

  • 文本文字(Text literals): 'one text','Another one!',…

  • 数字文本(Number literals): 0,34,3.0,12.3,…

  • 布尔文本(Boolean literals): true,false

  • 空(Null literal): null

  • 文字标记(Literal tokens): one,sometext,main,…

文本操作(Text operations)

  • 字符串连接(String concatenation): +

  • 文本替换(Literal substitutions): |Thenameis${name}|

算术运算(Arithmetic operations)

  • 二元运算符(Binary operators): +,-,*,/,%

  • 减号(单目运算符)Minus sign (unary operator): -

布尔操作(Boolean operations)

  • 二元运算符(Binary operators): and,or

  • 布尔否定(一元运算符)Boolean negation (unary operator): !,not

比较和等价(Comparisons and equality)

  • 比较(Comparators): >,<,>=,<=(gt,lt,ge,le)

  • 等值运算符(Equality operators): ==,!=(eq,ne)

条件运算符(Conditional operators)

  • If-then: (if)?(then)

  • If-then-else: (if)?(then):(else)

  • Default: (value) ?: (defaultvalue)

所有这些特征可以被组合并嵌套:

  1. 'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

常用th标签都有那些?

关键字功能介绍案例
th:id 替换id <inputth:id="'xxx' + ${collect.id}"/>
th:text 文本替换 <pth:text="${collect.description}">description</p>
th:utext 支持html的文本替换 <pth:utext="${htmlcontent}">conten</p>
th:object 替换对象 <divth:object="${session.user}">
th:value 属性赋值 <inputth:value="${user.name}"/>
th:with 变量赋值运算 <divth:with="isEven=${prodStat.count}%2==0"></div>
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 点击事件 th:onclick="'getCollect()'"
th:each 属性赋值 tr th:each="user,userStat:${users}">
th:if 判断条件 <ath:if="${userId == collect.userId}">
th:unless 和th:if判断相反 <ath:href="@{/login}"th:unless=${session.user!=null}>Login</a>
th:href 链接地址 <ath:href="@{/login}"th:unless=${session.user!=null}>Login</a>/>
th:switch 多路选择 配合th:case 使用 <divth:switch="${user.role}">
th:case th:switch的一个分支 <pth:case="'admin'">User is an administrator</p>
th:fragment 布局标签,定义一个代码片段,方便其它地方引用 <divth:fragment="alert">
th:include 布局标签,替换内容到引入的文件 <headth:include="layout :: htmlhead"th:with="title='xx'"></head>/>
th:replace 布局标签,替换整个标签到引入的文件 <divth:replace="fragments/header :: title"></div>
th:selected selected选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"
th:src 图片类地址引入 <imgclass="img-responsive"alt="App Logo"th:src="@{/img/logo.png}"/>
th:inline 定义js脚本可以使用变量 <scripttype="text/javascript"th:inline="javascript">
th:action 表单提交的地址 <formaction="subscribe.html"th:action="@{/subscribe}">
th:remove 删除某个属性 <trth:remove="all">1.all:删除包含标签和所有的孩子。2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。
th:attr 设置标签属性,多个属性可以用逗号分隔 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。

还有非常多的标签,这里只列出最常用的几个,由于一个标签内可以包含多个th:x属性,其生效的优先级顺序为: include,each,if/unless/switch/case,with,attr/attrprepend/attrappend,value/href,src,etc,text/utext,fragment,remove

几种常用的使用方法

1、赋值、字符串拼接

  1. <p th:text="${collect.description}">description</p>

  2. <span th:text="'Welcome to our application, ' + ${user.name} + '!'">

字符串拼接还有另外一种简洁的写法

  1. <span th:text="|Welcome to our application, ${user.name}!|">

2、条件判断 If/Unless

Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中, <a>标签只有在 th:if中条件成立时才显示:

  1. <a th:if="${myself=='yes'}" > </i> </a>

  2. <a th:unless=${session.user != null} th:href="@{/login}" >Login</a>

th:unless 于 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。

也可以使用 (if)?(then):(else)这种语法来判断显示的内容

3、for 循环

  1. <tr th:each="collect,iterStat : ${collects}">

  2. <th scope="row" th:text="${collect.id}">1</th>

  3. <td >

  4. <img th:src="${collect.webLogo}"/>

  5. </td>

  6. <td th:text="${collect.url}">Mark</td>

  7. <td th:text="${collect.title}">Otto</td>

  8. <td th:text="${collect.description}">@mdo</td>

  9. <td th:text="${terStat.index}">index</td>

  10. </tr>

iterStat称作状态变量,属性有:

  • index:当前迭代对象的 index(从0开始计算)

  • count: 当前迭代对象的 index(从1开始计算)

  • size:被迭代对象的大小

  • current:当前迭代变量

  • even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)

  • first:布尔值,当前循环是否是第一个

  • last:布尔值,当前循环是否是最后一个

4、URL

URL 在 Web 应用模板中占据着十分重要的地位,需要特别注意的是 Thymeleaf 对于 URL 的处理是通过语法 @{...} 来处理的。 如果需要 Thymeleaf 对 URL 进行渲染,那么务必使用 th:href, th:src 等属性,下面是一个例子

  1. <!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) -->

  2. <a th:href="@{/standard/{type}(type=${type})}">view</a>

  3.  

  4. <!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->

  5. <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

设置背景

  1. <div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div>

根据属性值改变背景

  1. <div class="media-object resource-card-image" th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div>

几点说明:

  • 上例中 URL 最后的 (orderId=${o.id})表示将括号内的内容作为 URL 参数处理,该语法避免使用字符串拼接,大大提高了可读性

  • @{...}表达式中可以通过 {orderId}访问 Context 中的 orderId 变量

  • @{/order}是 Context 相关的相对路径,在渲染时会自动添加上当前 Web 应用的 Context 名字,假设 context 名字为 app,那么结果应该是 /app/order

5、内联 js

内联文本:[[...]] 内联文本的表示方式,使用时,必须先用 th:inline="text/javascript/none"激活, th:inline可以在父级标签内使用,甚至作为 body 的标签。内联文本尽管比 th:text的代码少,不利于原型显示。

  1. <script th:inline="javascript">

  2. /*<![CDATA[*/

  3. ...

  4. var username = /*[[${sesion.user.name}]]*/ 'Sebastian';

  5. var size = /*[[${size}]]*/ 0;

  6. ...

  7. /*]]>*/

  8. </script>

js 附加代码:

  1. /*[+

  2. var msg = 'This is a working application';

  3. +]*/

js 移除代码:

  1. /*[- */

  2. var msg = 'This is a non-working template';

  3. /* -]*/

6、内嵌变量

为了模板更加易用,Thymeleaf 还提供了一系列 Utility 对象(内置于 Context 中),可以通过 # 直接访问:

  • dates : java.util.Date的功能方法类。

  • calendars : 类似#dates,面向java.util.Calendar

  • numbers : 格式化数字的功能方法类

  • strings : 字符串对象的功能类,contains,startWiths,prepending/appending等等。

  • objects: 对objects的功能类操作。

  • bools: 对布尔值求值的功能方法。

  • arrays:对数组的功能类方法。

  • lists: 对lists功能类方法

  • sets

  • maps
    ...

下面用一段代码来举例一些常用的方法:

dates

  1. /*

  2. * Format date with the specified pattern

  3. * Also works with arrays, lists or sets

  4. */

  5. ${#dates.format(date, 'dd/MMM/yyyy HH:mm')}

  6. ${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}

  7. ${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}

  8. ${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}

  9.  

  10. /*

  11. * Create a date (java.util.Date) object for the current date and time

  12. */

  13. ${#dates.createNow()}

  14.  

  15. /*

  16. * Create a date (java.util.Date) object for the current date (time set to 00:00)

  17. */

  18. ${#dates.createToday()}

strings

  1. /*

  2. * Check whether a String is empty (or null). Performs a trim() operation before check

  3. * Also works with arrays, lists or sets

  4. */

  5. ${#strings.isEmpty(name)}

  6. ${#strings.arrayIsEmpty(nameArr)}

  7. ${#strings.listIsEmpty(nameList)}

  8. ${#strings.setIsEmpty(nameSet)}

  9.  

  10. /*

  11. * Check whether a String starts or ends with a fragment

  12. * Also works with arrays, lists or sets

  13. */

  14. ${#strings.startsWith(name,'Don')} // also array*, list* and set*

  15. ${#strings.endsWith(name,endingFragment)} // also array*, list* and set*

  16.  

  17. /*

  18. * Compute length

  19. * Also works with arrays, lists or sets

  20. */

  21. ${#strings.length(str)}

  22.  

  23. /*

  24. * Null-safe comparison and concatenation

  25. */

  26. ${#strings.equals(str)}

  27. ${#strings.equalsIgnoreCase(str)}

  28. ${#strings.concat(str)}

  29. ${#strings.concatReplaceNulls(str)}

  30.  

  31. /*

  32. * Random

  33. */

  34. ${#strings.randomAlphanumeric(count)}

使用 Thymeleaf 布局

Spring Boot 2.0 将布局单独提取了出来,需要单独引入依赖:thymeleaf-layout-dialect。

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>nz.net.ultraq.thymeleaf</groupId>

  7. <artifactId>thymeleaf-layout-dialect</artifactId>

  8. </dependency>

定义代码片段

  1. <footer th:fragment="copy">

  2. &copy; 2019

  3. </footer>

在页面任何地方引入:

  1. <body>

  2. <div th:insert="layout/copyright :: copyright"></div>

  3. <div th:replace="layout/copyright :: copyright"></div>

  4. </body>

th:insert 和 th:replace 区别,insert 只是加载,replace 是替换。Thymeleaf 3.0 推荐使用 th:insert 替换 2.0 的 th:replace。

返回的 HTML 如下:

  1. <body>

  2. <div> &copy; 2019 </div>

  3. <footer>&copy; 2019 </footer>

  4. </body>

下面是一个常用的后台页面布局,将整个页面分为头部,尾部、菜单栏、隐藏栏,点击菜单只改变 content 区域的页面

  1. <body class="layout-fixed">

  2. <div th:fragment="navbar" class="wrapper" role="navigation">

  3. <div th:replace="fragments/header :: header">Header</div>

  4. <div th:replace="fragments/left :: left">left</div>

  5. <div th:replace="fragments/sidebar :: sidebar">sidebar</div>

  6. <div layout:fragment="content" id="content" ></div>

  7. <div th:replace="fragments/footer :: footer">footer</div>

  8. </div>

  9. </body>

任何页面想使用这样的布局值只需要替换中见的 content 模块即可

  1. <html xmlns:th="http://www.thymeleaf.org" layout:decorator="layout">

  2. <body>

  3. <section layout:fragment="content">

  4. ...

也可以在引用模版的时候传参

  1. <head th:include="layout :: htmlhead" th:with="title='Hello'"></head>

layout 是文件地址,如果有文件夹可以这样写 fileName/layout:htmlhead,htmlhead 是指定义的代码片段 如 th:fragment="copy"

文章示例项目

示例代码-https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf

文章内容已经升级到 Spring Boot 2.x

精彩回顾:

面试点:Java 中 hashCode() 和 equals() 的关系

容器连接[Docker 系列-7]

现如今的技术浪潮中,我们到底该做些什么?

 

强烈推荐:

《Java 极客技术》知识星球限时优惠,现在加入只需 50 元,仅限前 1000 名,机不可失时不再来。趁早行动吧!

https://t.zsxq.com/J6Em2nU

 

隆重介绍:

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

 

 

posted @ 2019-06-27 16:40  Java极客技术  阅读(255)  评论(0编辑  收藏  举报