Thymeleaf對象的使用:字符串對象


Thymeleaf主要使用 org.thymeleaf.expression.Strings 類處理字符串,在模板中使用 #strings 對象來處理字符串。

開發環境: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

調用參數的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>
判斷是否包含(區分大小寫)
<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>
判斷開頭和結尾是否包含(區分大小寫)
<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>
根據分隔符將每個單詞的首字母轉換為大寫
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(參數個數不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
從第二個參數之后拼接字符串,如果參數為null,則用第一個參數替代
<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>
產生指定位數的隨機字母數字,范圍為大寫英文字母加0-9數字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
調用HtmlEscape類的escapeHtml4Xml方法對參數進行編碼
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>

瀏覽器訪問:http://localhost:8080
頁面輸出:

調用參數的toString方法返回字符串
hello
返回字符串的長度
5
判斷是否為空或null
false
true
true
為空或null時設置默認值
hello
b
c
判斷是否包含(區分大小寫)
true
false
判斷是否包含(忽略大小寫)
true
true
判斷開頭和結尾是否包含(區分大小寫)
true
false
false
true
獲取字符串的索引(如果不存在返回-1)
1
-1
指定開始和結束索引,截取字符串(如果索引超過字符串長度,則拋出異常)
el
指定從某個字符串后面截取字符串(如果不包含則返回空字符串)
llo
指定從某個字符串前面截取字符串(如果不包含則返回空字符串)
h
替換字符串
hallo
轉換為大寫
HELLO
轉換為小寫
hello
首字母轉換為大寫
Hello
首字母轉換為小寫
heLLo
每個單詞的首字母轉為大寫
Hello World
根據分隔符將每個單詞的首字母轉換為大寫
Hello-World
字符串前面追加
hello world
字符串后面追加
hello world
拼接字符串(參數個數不限)
hello world !
從第二個參數之后拼接字符串,如果參數為null,則用第一個參數替代
hello*world
刪除空白
hello
字符串截取指定長度(最小為3),后面加...
hello...
產生指定位數的隨機字母數字,范圍為大寫英文字母加0-9數字
PBAT
調用HtmlEscape類的escapeHtml4Xml方法對參數進行編碼
&lt;span&gt;hello&lt;/span&gt;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM