Thymeleaf常用語法:表達式語法之運算符


Thymeleaf表達式語法之常量分為字符串常量、數字常量、布爾值常量、空值常量;
運算符分為算術運算符、關系運算符、條件運算符、無操作符。

開發環境: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/java/com/example/demo/TestController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/")
    public String test(Model model){
        model.addAttribute("flag", true);
        return "test";
    }
}

3、src/main/resources/templates/test.html

<h4>一、表達式常量</h4>
1、字符串常量:使用單引號,或|
<div th:text="'Hello,World'"></div>
<div th:text="|Hello,World|"></div>
如果雙引號內容只包含“a-zA-Z0-9[]-_”,也可省略單引號,別的情況如使用逗號會發生異常
<div th:text="HelloWorld"></div>
2、數字常量
<div th:text="10"></div>
<div th:text="10 + 20"></div>
3、布爾值常量:==true語句放到${...}外面則由Thymeleaf處理,放在里面則由OGNL或SpringEL處理
<div th:if="${flag} == true">顯示</div>
<div th:if="${flag == false}">隱藏</div>
4、空值常量
<div th:if="${flag} == null">顯示</div>
<div th:if="${flag == null}">隱藏</div>
5、字符串拼接:除了用#strings對象的append和concat方法,也可使用“+”號、“|”符號
<div th:text="a + b"></div>
<div th:text="'a' + 'b'"></div>
<div th:text="${'a' + 'b'}"></div>
<div th:text="|a| + |b|"></div>

<h4>二、算術運算符</h4>
1、加法
<div th:text="${1 + 2}"></div>
<div th:text="1 + 2"></div>
2、減法
<div th:text="${1 - 2}"></div>
<div th:text="1 - 2"></div>
3、乘法
<div th:text="${1 * 2}"></div>
<div th:text="1 * 2"></div>
4、除法:除法還可以用別名 div
<div th:text="${1 / 2}"></div>
<div th:text="${1 div 2}"></div>
<div th:text="${1 / 2.0}"></div>
<div th:text="1 / 2"></div>
5、求余:求余還可以用別名 mod
<div th:text="${1 % 2}"></div>
<div th:text="${1 mod 2}"></div>
<div th:text="1 % 2"></div>

<h4>三、關系運算符</h4>
關系運算符有: >、<>=、<=、==、!=,
<div>對應的別名是:gt、lt、ge、le、eq、ne</div>
<div th:text="1 > 1"></div>
<div th:text="1 gt 1"></div>
<div th:text="1 < 1"></div>
<div th:text="1 lt 1"></div>
<div th:text="1 >= 1"></div>
<div th:text="1 ge 1"></div>
<div th:text="1 <= 1"></div>
<div th:text="1 le 1"></div>
<div th:text="1 == 1"></div>
<div th:text="1 eq 1"></div>
<div th:text="1 != 1"></div>
<div th:text="1 ne 1"></div>

<h4>三、條件運算符</h4>
1、條件運算符表達式為:(condition) ? then : else
<div th:text="${1 > 1} ? |大於| : |不大於|"></div>
<div th:text="1 > 1 ? |大於| : |不大於|"></div>
也可省略then
<div th:text="1 > 1 ? |大於|"></div>
2、默認值表達式為:(value)?:(defaultValue),表示存在某個值時直接返回該值,否則返回默認值
<div th:text="${'a'} ?: |一|"></div>
<div th:text="${null} ?: |一|"></div>

<h4>四、無操作符</h4>
使用“_”表示無操作,當一個值不存在時,使用該符號指定表達式不進行任何操作,這樣對原型破壞最小。
例如原型為:<div>abc</div>
可能會使用語句:<div th:text="${userName} ?: 'abc'"></div>
現在可使用:<div th:text="${userName} ?: _">abc</div>

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

一、表達式常量
1、字符串常量:使用單引號,或|
Hello,World
Hello,World
如果雙引號內容只包含“a-zA-Z0-9[]-_”,也可省略單引號,別的情況如使用逗號會發生異常
HelloWorld
2、數字常量
10
30
3、布爾值常量:==true語句放到${...}外面則由Thymeleaf處理,放在里面則由OGNL或SpringEL處理
顯示
4、空值常量 5、字符串拼接:除了用#strings對象的append和concat方法,也可使用“+”號、“|”符號
ab
ab
ab
ab
二、算術運算符
1、加法
3
3
2、減法
-1
-1
3、乘法
2
2
4、除法:除法還可以用別名 div
0
0
0.5
0.5
5、求余:求余還可以用別名 mod
1
1
1
三、關系運算符
關系運算符有: >、<、>=、<=、==、!=,
對應的別名是:gt、lt、ge、le、eq、ne
false
false
false
false
true
true
true
true
true
true
false
false
三、條件運算符
1、條件運算符表達式為:(condition) ? then : else
不大於
不大於
也可省略then
2、默認值表達式為:(value)?:(defaultValue),表示存在某個值時直接返回該值,否則返回默認值
a
一
四、無操作符
使用“_”表示無操作,當一個值不存在時,使用該符號指定表達式不進行任何操作,這樣對原型破壞最小。 例如原型為:
abc
可能會使用語句:
abc
現在可使用:
abc

 


免責聲明!

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



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