Thymeleaf運(yùn)算符怎么用

這篇文章主要介紹“Thymeleaf運(yùn)算符怎么用”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Thymeleaf運(yùn)算符怎么用”文章能幫助大家解決問題。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比南票網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式南票網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋南票地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。

Thymeleaf表達(dá)式語法之常量分為字符串常量、數(shù)字常量、布爾值常量、空值常量;
運(yùn)算符分為算術(shù)運(yùn)算符、關(guān)系運(yùn)算符、條件運(yùn)算符、無操作符。

開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。

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

<h5>一、表達(dá)式常量</h5>
1、字符串常量:使用單引號(hào),或|
<div th:text="'Hello,World'"></div>
<div th:text="|Hello,World|"></div>
如果雙引號(hào)內(nèi)容只包含“a-zA-Z0-9[]-_”,也可省略單引號(hào),別的情況如使用逗號(hào)會(huì)發(fā)生異常
<div th:text="HelloWorld"></div>
2、數(shù)字常量
<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對(duì)象的append和concat方法,也可使用“+”號(hào)、“|”符號(hào)
<div th:text="a + b"></div>
<div th:text="'a' + 'b'"></div>
<div th:text="${'a' + 'b'}"></div>
<div th:text="|a| + |b|"></div>

<h5>二、算術(shù)運(yùn)算符</h5>
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>

<h5>三、關(guān)系運(yùn)算符</h5>
關(guān)系運(yùn)算符有: >、<、>=、<=、==、!=,
<div>對(duì)應(yīng)的別名是: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>

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

<h5>四、無操作符</h5>
使用“_”表示無操作,當(dāng)一個(gè)值不存在時(shí),使用該符號(hào)指定表達(dá)式不進(jìn)行任何操作,這樣對(duì)原型破壞最小。
例如原型為:<div>abc</div>
可能會(huì)使用語句:<div th:text="${userName} ?: 'abc'"></div>
現(xiàn)在可使用:<div th:text="${userName} ?: _">abc</div>

瀏覽器訪問:http://localhost:8080
頁(yè)面輸出:

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

關(guān)于“Thymeleaf運(yùn)算符怎么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

當(dāng)前題目:Thymeleaf運(yùn)算符怎么用
標(biāo)題URL:http://bm7419.com/article10/jcsido.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站導(dǎo)航網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)、服務(wù)器托管、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司