spring boot 中 thymeleaf基本的用法
标准表达式
- 变量表达式:${ }
- 选择表达式:*{ }
- URL链接表达式:@{ }
- 条件表达式:三目运算符(表达式 ?值(then):值(else))
- 默认表达式:不带then(默认值为表达式的值,表达式 ?:值(else,表达式为null时))
常用语法
1.赋值、字符串拼接
<p th:text="${userName}">neo</p>
<span th:text="'Welcome to our application, ' + ${userName} + '!'"></span>
<span th:text="|Welcome to our application, ${userName}!|"></span>
2.条件判断
- If/Unless
<a th:if="${flag == 'yes'}" th:href="@{/home}"> home </a>
<a th:unless="${flag != 'no'}" th:href="@{http://www.ityouknow.com/}" >ityouknow</a>
th:unless 与 th:if 相反,只有表达式中的条件不成立,才会显示其内容。
也可以使用 (if) ? (then) : (else) 来判断显示的内容。
- Switch
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
</div>
3.for 循环
<table>
<tr th:each="user,iterStat : ${users}">
<td th:text="${user.name}">neo</td>
<td th:text="${user.age}">6</td>
<td th:text="${user.pass}">213</td>
<td th:text="${iterStat.index}">index</td>
</tr>
</table>
iterStat 称作状态变量,属性有
- index:当前迭代对象的 index(从 0 开始计算)
- count:当前迭代对象的 index(从 1 开始计算)
- size:被迭代对象的大小
- current:当前迭代变量
- even/odd:布尔值,当前循环是否是偶数/奇数(从 0 开始计算)
- first:布尔值,当前循环是否是第一个
- last:布尔值,当前循环是否是最后一个
4.URL
Thymeleaf 对于 URL 的处理是通过语法 @{…} 来处理的。若需要 Thymeleaf 对 URL 进行渲染,那务必使用 th:href、th:src 等属性。
<a th:href="@{http://www.ityouknow.com/{type}(type=${type})}">link1</a>
<a href="details.html" th:href="@{http://www.ityouknow.com/{pageId}/can-use-springcloud.html(pageId=${pageId})}">view</a>
设置背景:
<div th:style="'background:url(' + @{${img}} + ');'">
根据属性值改变背景:
<div class="media-object resource-card-image" th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div>
5.内联 [ [ ] ]
使用时,必须先用在 th:inline=“text/javascript/none” 激活,th:inline 可以在父级标签内使用,甚至作为 body 的标签。
文本内联:
<div th:inline="text" >
<h1>内联js</h1>
<p>Hello, [[${userName}]]</p>
<br/>
</div>
脚本内联:
<script th:inline="javascript">
var name = [[${userName}]] + ', Sebastian';
alert(name);
</script>
脚本内联可以在 js 中取到后台传过来的参数。
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
下面用一段代码来举例一些常用的方法:
date
/*
* Format date with the specified pattern
* Also works with arrays, lists or sets
*/
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}
${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}
${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}
/*
* Create a date (java.util.Date) object for the current date and time
*/
${#dates.createNow()}
/*
* Create a date (java.util.Date) object for the current date (time set to 00:00)
*/
${#dates.createToday()}
string
/*
* Check whether a String is empty (or null). Performs a trim() operation before check
* Also works with arrays, lists or sets
*/
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}
/*
* Check whether a String starts or ends with a fragment
* Also works with arrays, lists or sets
*/
${#strings.startsWith(name,'Don')} // also array*, list* and set*
${#strings.endsWith(name,endingFragment)} // also array*, list* and set*
/*
* Compute length
* Also works with arrays, lists or sets
*/
${#strings.length(str)}
/*
* Null-safe comparison and concatenation
*/
${#strings.equals(str)}
${#strings.equalsIgnoreCase(str)}
${#strings.concat(str)}
${#strings.concatReplaceNulls(str)}
/*
* Random
*/
${#strings.randomAlphanumeric(count)}
spring-boot1.4之后,支持thymeleaf3,可以更改版本号来进行修改支持!