Springboot Thymeleaf字符串對象實例解析
Thymeleaf主要使用 org.thymeleaf.expression.Strings 類處理字符串,在模板中使用 #strings 對象來處理字符串。
開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一個名稱為demo的Spring Boot項目。
1、pom.xml
加入Thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、src/main/resources/application.yml
設置模板緩存為false,這樣修改html頁面后刷新瀏覽器能馬上看到結(jié)果
spring:
thymeleaf:
cache: false
3、src/main/java/com/example/demo/TestController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/")
public String test(){
return "test";
}
}
4、src/main/resources/templates/test.html
調(diào)用參數(shù)的toString方法返回字符串
<div th:text="${#strings.toString('hello')}"></div>
返回字符串的長度
<div th:text="${#strings.length('hello')}"></div>
判斷是否為空或null
<div th:text="${#strings.isEmpty('hello')}"></div>
<div th:text="${#strings.isEmpty('')}"></div>
<div th:text="${#strings.isEmpty(null)}"></div>
為空或null時設置默認值
<div th:text="${#strings.defaultString('hello','a')}"></div>
<div th:text="${#strings.defaultString('','b')}"></div>
<div th:text="${#strings.defaultString(null,'c')}"></div>
判斷是否包含(區(qū)分大小寫)
<div th:text="${#strings.contains('hello','he')}"></div>
<div th:text="${#strings.contains('hello','HE')}"></div>
判斷是否包含(忽略大小寫)
<div th:text="${#strings.containsIgnoreCase('hello','he')}"></div>
<div th:text="${#strings.containsIgnoreCase('hello','HE')}"></div>
判斷開頭和結(jié)尾是否包含(區(qū)分大小寫)
<div th:text="${#strings.startsWith('hello','he')}"></div>
<div th:text="${#strings.startsWith('hello','HE')}"></div>
<div th:text="${#strings.startsWith('hello','el')}"></div>
<div th:text="${#strings.endsWith('hello','lo')}"></div>
獲取字符串的索引(如果不存在返回-1)
<div th:text="${#strings.indexOf('hello','el')}"></div>
<div th:text="${#strings.indexOf('hello','ee')}"></div>
指定開始和結(jié)束索引,截取字符串(如果索引超過字符串長度,則拋出異常)
<div th:text="${#strings.substring('hello',1,3)}"></div>
指定從某個字符串后面截取字符串(如果不包含則返回空字符串)
<div th:text="${#strings.substringAfter('hello','e')}"></div>
<div th:text="${#strings.substringAfter('hello','ee')}"></div>
指定從某個字符串前面截取字符串(如果不包含則返回空字符串)
<div th:text="${#strings.substringBefore('hello','e')}"></div>
<div th:text="${#strings.substringBefore('hello','ee')}"></div>
替換字符串
<div th:text="${#strings.replace('hello','e','a')}"></div>
轉(zhuǎn)換為大寫
<div th:text="${#strings.toUpperCase('hello')}"></div>
轉(zhuǎn)換為小寫
<div th:text="${#strings.toLowerCase('HELLO')}"></div>
首字母轉(zhuǎn)換為大寫
<div th:text="${#strings.capitalize('hello')}"></div>
首字母轉(zhuǎn)換為小寫
<div th:text="${#strings.unCapitalize('heLLo')}"></div>
每個單詞的首字母轉(zhuǎn)為大寫
<div th:text="${#strings.capitalizeWords('hello world')}"></div>
根據(jù)分隔符將每個單詞的首字母轉(zhuǎn)換為大寫
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(參數(shù)個數(shù)不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
從第二個參數(shù)之后拼接字符串,如果參數(shù)為null,則用第一個參數(shù)替代
<div th:text="${#strings.concatReplaceNulls('*','hello',null,'world')}"></div>
刪除空白
<div th:text="${#strings.trim(' hello ')}"></div>
字符串截取指定長度(最小為3),后面加...
<div th:text="${#strings.abbreviate('hello,world', 8)}"></div>
產(chǎn)生指定位數(shù)的隨機字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
調(diào)用HtmlEscape類的escapeHtml4Xml方法對參數(shù)進行編碼
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>
瀏覽器訪問:http://localhost:8080
頁面輸出:
調(diào)用參數(shù)的toString方法返回字符串 hello 返回字符串的長度 判斷是否為空或null false true true 為空或null時設置默認值 hello b c 判斷是否包含(區(qū)分大小寫) true false 判斷是否包含(忽略大小寫) true true 判斷開頭和結(jié)尾是否包含(區(qū)分大小寫) true false false true 獲取字符串的索引(如果不存在返回-1) -1 指定開始和結(jié)束索引,截取字符串(如果索引超過字符串長度,則拋出異常) el 指定從某個字符串后面截取字符串(如果不包含則返回空字符串) llo 指定從某個字符串前面截取字符串(如果不包含則返回空字符串) h 替換字符串 hallo 轉(zhuǎn)換為大寫 HELLO 轉(zhuǎn)換為小寫 hello 首字母轉(zhuǎn)換為大寫 Hello 首字母轉(zhuǎn)換為小寫 heLLo 每個單詞的首字母轉(zhuǎn)為大寫 Hello World 根據(jù)分隔符將每個單詞的首字母轉(zhuǎn)換為大寫 Hello-World 字符串前面追加 hello world 字符串后面追加 hello world 拼接字符串(參數(shù)個數(shù)不限) hello world ! 從第二個參數(shù)之后拼接字符串,如果參數(shù)為null,則用第一個參數(shù)替代 hello*world 刪除空白 hello 字符串截取指定長度(最小為3),后面加... hello... 產(chǎn)生指定位數(shù)的隨機字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字 PBAT 調(diào)用HtmlEscape類的escapeHtml4Xml方法對參數(shù)進行編碼 <span>hello</span>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合Redis實現(xiàn)刷票過濾功能
隨著互聯(lián)網(wǎng)的不斷發(fā)展,網(wǎng)站或APP的用戶流量增加,也衍生出了一些惡意刷量等問題,給數(shù)據(jù)分析及運營帶來極大的困難,所以本文使用SpringBoot和Redis實現(xiàn)一個刷票過濾功能,需要的可以參考一下2023-06-06
Java 實戰(zhàn)項目錘煉之網(wǎng)上圖書館管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+jsp+servlet+mysql+ajax實現(xiàn)一個網(wǎng)上圖書館管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
SpringBoot整合Sa-Token實現(xiàn)登錄認證的示例代碼
本文主要介紹了SpringBoot整合Sa-Token實現(xiàn)登錄認證的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

