[JAVA EE] Thymeleaf 常用工具类
Thymeleaf 提供了豐富的表達(dá)式工具類,例如:
#strings:字符串工具類
#dates:時(shí)間操作和時(shí)間格式化
#numbers:格式化數(shù)字對(duì)象的方法
#bools:常用的布爾方法
#strings 工具類
? 字符串長(zhǎng)度(length)
? 字符串轉(zhuǎn)換(toString)
? 檢查字符串是否為空(isEmpty)
? 字符串是為空替換操作(defaultString)
? 檢查字符串中是否包含某個(gè)字符串(contains containsIgnoreCase)
? 檢查字符串是以片段開(kāi)頭還是結(jié)尾(startsWith endsWith)
? 截取(substring substringAfter substringBefore)
? 替換(replace)
? 追加(prepend append)
? 變更大小寫(xiě)(toUpperCase toLowerCase)
? 去空格(trim)
? 拆分和組合字符串(arrayJoin arraySplit)
示例:
如果值為 null 或 空串,則 true,否則 false
<p th:text="${#strings.isEmpty(stu.name)}"></p>
如果值為 null 或 空串,則顯示默認(rèn)值張三
<p th:text="${#strings.defaultString(stu.name,'張三')}"></p>
<p th:text="${#strings.length(stu.name)}"></p> <!-- 2 (stu.name='小明') -->
<p th:text="${#strings.contains('abcez','ez')}"></p> <!--true-->
<p th:text="${#strings.startsWith('Donabcez','Don')}"></p> <!--true-->
<p th:text="${#strings.indexOf('abcefg','e')}"></p> <!--3-->
最后一個(gè)參數(shù)<=總長(zhǎng)度
<p th:text="${#strings.substring('abcefg',3,5)}"></p> <!--ef-->
<p th:text="${#strings.replace('aabbccbb','bb','zz')}"></p> <!--aazzcczz-->
<p th:text="${#strings.prepend('88888888','027-')}"></p> <!--027-88888888-->
<p th:text="${#strings.append('abc','123')}"></p> <!--abc123-->
<p th:text="${#strings.toUpperCase('abc')}"></p> <!--ABC-->
<p th:text="${#strings.trim(' abc ')}"></p> <!--abc-->
#dates 工具類
? 格式化操作(format)
? 獲取日期屬性操作(day month year hour minute second monthName
dayOfWeek )
? 生成日期操作(createNow create createToday)
示例:
后臺(tái)添加代碼:model.addAttribute(“today”,new Date());
<p th:text="${today}"></p>
<p th:text="${#dates.format(today,'yyyy/MM/dd HH:mm:ss')}"></p>
<p th:text="${#dates.year(today)}"></p>
<p th:text="${#dates.month(today)}"></p>
<p th:text="${#dates.monthName(today)}"></p>
<p th:text="${#dates.day(today)}"></p>
<p th:text="${#dates.dayOfWeek(today)}"></p>
<p th:text="${#dates.dayOfWeekName(today)}"></p>
<p th:text="${#dates.hour(today)}"></p>
<p th:text="${#dates.minute(today)}"></p>
<p th:text="${#dates.second(today)}"></p>
<p th:text="${#dates.createNow()}"></p> <!--含有時(shí)間-->
<p th:text="${#dates.createToday()}"></p> <!--時(shí)間為00:00:00-->
<p th:text="${#dates.create('2021','03','15')}"></p>
#numbers 工具類
? 對(duì)不夠位數(shù)的數(shù)字進(jìn)行補(bǔ)0(formatInteger )
? 設(shè)置千位分隔符(formatInteger)
? 精確小數(shù)點(diǎn)(formatDecimal )
? 設(shè)置百分號(hào)(formatPercent )
? 生成數(shù)組(sequence )
示例:
結(jié)果:010.13
<p th:text="${#numbers.formatDecimal('10.126',3,2)}"></p>
結(jié)果:¥1,000.00
<p th:text="${#numbers.formatCurrency('1000')}"></p>
生成 [0,1,2,3,4] 數(shù)組
<div th:each="n:${#numbers.sequence(0,4)}"><p th:text="${n}"></p>
</div>
生成 [0,2,4,6,8,10] 數(shù)組
<div th:each="num:${#numbers.sequence(0,10,2)}" ><p th:text="${num}"></p>
</div>
#bools 工具類
? 判斷對(duì)象是否為 ture 或 false的操作:(isTrue isFalse)
? 數(shù)字 1 為 ture , 0 為 false;
? “on” 為 true, “off” 為 false;
? “true” 為 true, "false"為 false;
示例:
<p th:text="${#bools.isTrue(1)}"></p> <!--true-->
<p th:text="${#bools.isTrue('on')}"></p> <!--true-->
<p th:text="${#bools.isTrue('true')}"></p> <!--true-->
總結(jié)
以上是生活随笔為你收集整理的[JAVA EE] Thymeleaf 常用工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 绿树村边合的下一句是什么呢?
- 下一篇: [JAVA EE] 内联用法