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頁面后刷新瀏覽器能馬上看到結果
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
調用參數(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> 判斷開頭和結尾是否包含(區(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> 指定開始和結束索引,截取字符串(如果索引超過字符串長度,則拋出異常) <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> 轉換為大寫 <div th:text="${#strings.toUpperCase('hello')}"></div> 轉換為小寫 <div th:text="${#strings.toLowerCase('HELLO')}"></div> 首字母轉換為大寫 <div th:text="${#strings.capitalize('hello')}"></div> 首字母轉換為小寫 <div th:text="${#strings.unCapitalize('heLLo')}"></div> 每個單詞的首字母轉為大寫 <div th:text="${#strings.capitalizeWords('hello world')}"></div> 根據(jù)分隔符將每個單詞的首字母轉換為大寫 <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> 產生指定位數(shù)的隨機字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字 <div th:text="${#strings.randomAlphanumeric(4)}"></div> 調用HtmlEscape類的escapeHtml4Xml方法對參數(shù)進行編碼 <div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>
瀏覽器訪問:http://localhost:8080
頁面輸出:
調用參數(shù)的toString方法返回字符串 hello 返回字符串的長度 判斷是否為空或null false true true 為空或null時設置默認值 hello b c 判斷是否包含(區(qū)分大小寫) true false 判斷是否包含(忽略大小寫) true true 判斷開頭和結尾是否包含(區(qū)分大小寫) true false false true 獲取字符串的索引(如果不存在返回-1) -1 指定開始和結束索引,截取字符串(如果索引超過字符串長度,則拋出異常) el 指定從某個字符串后面截取字符串(如果不包含則返回空字符串) llo 指定從某個字符串前面截取字符串(如果不包含則返回空字符串) h 替換字符串 hallo 轉換為大寫 HELLO 轉換為小寫 hello 首字母轉換為大寫 Hello 首字母轉換為小寫 heLLo 每個單詞的首字母轉為大寫 Hello World 根據(jù)分隔符將每個單詞的首字母轉換為大寫 Hello-World 字符串前面追加 hello world 字符串后面追加 hello world 拼接字符串(參數(shù)個數(shù)不限) hello world ! 從第二個參數(shù)之后拼接字符串,如果參數(shù)為null,則用第一個參數(shù)替代 hello*world 刪除空白 hello 字符串截取指定長度(最小為3),后面加... hello... 產生指定位數(shù)的隨機字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字 PBAT 調用HtmlEscape類的escapeHtml4Xml方法對參數(shù)進行編碼 <span>hello</span>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java?ArrayList實現(xiàn)刪除指定位置的元素
目標:list中有0到39共40個元素,刪除其中索引是10、20、30的元素。本文為大家整理了三個不同的方法,感興趣的小伙伴可以跟隨小編一起學習一下2023-01-01Windows10系統(tǒng)下JDK1.8環(huán)境變量的配置
今天帶大家學習在Windows10系統(tǒng)下怎么配置JDK1.8環(huán)境變量,文中有非常詳細的安裝及配置教程,對正在學習的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05