日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

[JAVA EE] Thymeleaf 常用工具类

發布時間:2023/11/27 生活经验 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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 常用工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。