[JAVA EE] Thymeleaf 常用工具类
Thymeleaf 提供了豐富的表達式工具類,例如:
#strings:字符串工具類
#dates:時間操作和時間格式化
#numbers:格式化數字對象的方法
#bools:常用的布爾方法
#strings 工具類
? 字符串長度(length)
? 字符串轉換(toString)
? 檢查字符串是否為空(isEmpty)
? 字符串是為空替換操作(defaultString)
? 檢查字符串中是否包含某個字符串(contains containsIgnoreCase)
? 檢查字符串是以片段開頭還是結尾(startsWith endsWith)
? 截取(substring substringAfter substringBefore)
? 替換(replace)
? 追加(prepend append)
? 變更大小寫(toUpperCase toLowerCase)
? 去空格(trim)
? 拆分和組合字符串(arrayJoin arraySplit)
示例:
如果值為 null 或 空串,則 true,否則 false
<p th:text="${#strings.isEmpty(stu.name)}"></p>
如果值為 null 或 空串,則顯示默認值張三
<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-->
最后一個參數<=總長度
<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)
示例:
后臺添加代碼: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> <!--含有時間-->
<p th:text="${#dates.createToday()}"></p> <!--時間為00:00:00-->
<p th:text="${#dates.create('2021','03','15')}"></p>
#numbers 工具類
? 對不夠位數的數字進行補0(formatInteger )
? 設置千位分隔符(formatInteger)
? 精確小數點(formatDecimal )
? 設置百分號(formatPercent )
? 生成數組(sequence )
示例:
結果:010.13
<p th:text="${#numbers.formatDecimal('10.126',3,2)}"></p>
結果:¥1,000.00
<p th:text="${#numbers.formatCurrency('1000')}"></p>
生成 [0,1,2,3,4] 數組
<div th:each="n:${#numbers.sequence(0,4)}"><p th:text="${n}"></p>
</div>
生成 [0,2,4,6,8,10] 數組
<div th:each="num:${#numbers.sequence(0,10,2)}" ><p th:text="${num}"></p>
</div>
#bools 工具類
? 判斷對象是否為 ture 或 false的操作:(isTrue isFalse)
? 數字 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-->
總結
以上是生活随笔為你收集整理的[JAVA EE] Thymeleaf 常用工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绿树村边合的下一句是什么呢?
- 下一篇: [JAVA EE] 内联用法