解讀thymeleaf模板引擎中th-if的使用_第1頁
解讀thymeleaf模板引擎中th-if的使用_第2頁
解讀thymeleaf模板引擎中th-if的使用_第3頁
解讀thymeleaf模板引擎中th-if的使用_第4頁
解讀thymeleaf模板引擎中th-if的使用_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第解讀thymeleaf模板引擎中th:if的使用目錄thymeleaf模板引擎中th:if的使用th:if條件判斷th:if判斷表達(dá)式Thymeleaf模板引擎語法使用1、模板引擎thymeleaf使用2、ognl表達(dá)式的語法糖3、拼接4、運(yùn)算5、循環(huán)6、邏輯判斷7、分支控制switch8、JS模板

thymeleaf模板引擎中th:if的使用

th:if條件判斷

很多時(shí)候只有在滿?某個(gè)條件時(shí),才將?個(gè)模板?段顯示在結(jié)果中,否則不進(jìn)行顯示。比如只有當(dāng)用戶有訂單時(shí),才為它顯示訂單鏈接,否則不顯示。th:if屬性用于滿足這個(gè)需求

body

!--if屬性結(jié)果為true,模板會(huì)進(jìn)行顯示--

pth:if="true"th:if="true"/p

!--if屬性結(jié)果為false,模板不會(huì)進(jìn)行顯示--

pth:if="false"th:if="false"/p

!--后臺(tái)控制器傳出數(shù)據(jù):model.addAttribute("isMarry",true);--

pth:if="${isMarry}"已婚/p

/body

th:if屬性不僅只以布爾值作為判斷條件,它將按照如下規(guī)則判定指定的表達(dá)式結(jié)果為true:

如果表達(dá)式結(jié)果為布爾值,則為true或false如果表達(dá)式的值為null,th:if將判定此表達(dá)式為false如果值是數(shù)字,為0時(shí),判斷為false;不為零時(shí),判定為true如果值是是String,值為false、off、no時(shí),判定為false,否則判斷為true,字符串為空時(shí),也判斷為true如果值不是布爾值,數(shù)字,字符或字符串的其它對(duì)象,只要不為null,則判斷為true

body

!--表達(dá)式的結(jié)果為布爾類型時(shí),if直接以它為結(jié)果--

pth:if="true"th:if="true"/p

!--當(dāng)表達(dá)式結(jié)果為null時(shí),則if判定為false--

pth:if="null"th:if="null"/p

!--表達(dá)式為數(shù)字時(shí),不為0,if判定為true,為0,時(shí),if判定為false--

pth:if="11"th:if="11"/p

pth:if="0"th:if="0"/p

!--表達(dá)式結(jié)果為字符串時(shí),如果為true,則if判定為true;值為false,則if判定為false--

!--結(jié)果為off、no等特殊字符串時(shí),if判定為false--

pth:if="'true'"th:if="'true'"/p

pth:if="'false'"th:if="'false'"/p

pth:if="'off'"th:if="'off'"/p

pth:if="'no'"th:if="'no'"/p

!--表達(dá)式結(jié)果字符串不為false,off,no時(shí),if判定為true--

pth:if="'LoveChina'"th:if="'LoveChina'"/p

!--后臺(tái)傳輸:model.addAttribute("userList",User.getUsers());--

!--只要userList不等于null,則if判定為true,否則為false--

pth:if="${userList}"th:if="${userList}"/p

!--后臺(tái)傳輸:model.addAttribute("name","");--

!--字符串為空時(shí),if判定為true--

pth:if="${name}eq''"name等于空/p

pth:if="${name}"th:if="${name}"/p

/body

th:if判斷表達(dá)式

gt:(大于)ge:(大于等于)=eq:(等于)==lt:(小于)le:(小于等于)=ne:(不等于)!=

Thymeleaf模板引擎語法使用

1、模板引擎thymeleaf使用

引入依賴:

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-thymeleaf/artifactId

/dependency

頁面導(dǎo)入頭部文件:

htmlxmlns:th=""

語法說明:

Thymeleaf通過${}來獲取model中的變量,注意這不是el表達(dá)式,而是ognl表達(dá)式,但是語法非常像。

2、ognl表達(dá)式的語法糖

剛才獲取變量值,我們使用的是經(jīng)典的對(duì)象.屬性名方式。但有些情況下,我們的屬性名可能本身也是變量,怎么辦?

ognl提供了類似js的語法方式:

例如:${}可以寫作${user[name]}

自定義變量

場景

看下面的案例:

h2

pName:spanth:text="${}"Jack/span./p

pAge:spanth:text="${user.age}"21/span./p

pfriend:spanth:text="${}"Rose/span./p

/h2

我們獲取用戶的所有信息,分別展示。

當(dāng)數(shù)據(jù)量比較多的時(shí)候,頻繁的寫user.就會(huì)非常麻煩。

因此,Thymeleaf提供了自定義變量來解決:

示例:

h2th:object="${user}"

pName:spanth:text="*{name}"Jack/span./p

pAge:spanth:text="*{age}"21/span./p

pfriend:spanth:text="*{}"Rose/span./p

/h2

首先在h2上用th:object=${user}獲取user的值,并且保存

然后,在h2內(nèi)部的任意元素上,可以通過*{屬性名}的方式,來獲取user中的屬性,這樣就省去了大量的user.前綴了

3、拼接

我們經(jīng)常會(huì)用到普通字符串與表達(dá)式拼接的情況:

spanth:text="'歡迎您:'+${}+'!'"/span

字符串字面值需要用,拼接起來非常麻煩,Thymeleaf對(duì)此進(jìn)行了簡化,使用一對(duì)|即可:

spanth:text="|歡迎您:${}|"/span

與上面是完全等效的,這樣就省去了字符串字面值的書寫。

4、運(yùn)算

需要注意:${}內(nèi)部的是通過OGNL表達(dá)式引擎解析的,外部的才是通過Thymeleaf的引擎解析,因此運(yùn)算符盡量放在${}外進(jìn)行。

算術(shù)運(yùn)算

支持的算術(shù)運(yùn)算符:+-*/%

spanth:text="${user.age}"/span

spanth:text="${user.age}%2==0"/span

比較運(yùn)算

支持的比較運(yùn)算:,,=and=,但是,不能直接使用,因?yàn)閤ml會(huì)解析為標(biāo)簽,要使用別名。

注意==and!=不僅可以比較數(shù)值,類似于equals的功能。

可以使用的別名:gt(),lt(),ge(=),le(=),not(!).Alsoeq(==),neq/ne(!=).

條件運(yùn)算

三元運(yùn)算

spanth:text="${user.sex}'男':'女'"/span

三元運(yùn)算符的三個(gè)部分:conditonthen:else

?condition:條件?then:條件成立的結(jié)果?else:不成立的結(jié)果

其中的每一個(gè)部分都可以是Thymeleaf中的任意表達(dá)式。

默認(rèn)值

有的時(shí)候,我們?nèi)∫粋€(gè)值可能為空,這個(gè)時(shí)候需要做非空判斷,可以使用表達(dá)式:默認(rèn)值簡寫:

spanth:text="${}:'二狗'"/span

當(dāng)前面的表達(dá)式值為null時(shí),就會(huì)使用后面的默認(rèn)值。

注意::之間沒有空格。

5、循環(huán)

循環(huán)也是非常頻繁使用的需求,我們使用th:each指令來完成:

假如有用戶的集合:users在Context中。

trth:each="user:${users}"

tdth:text="${}"Onions/td

tdth:text="${user.age}"2.41/td

/tr

${users}是要遍歷的集合,可以是以下類型:Iterable,實(shí)現(xiàn)了Iterable接口的類Enumeration,枚舉Interator,迭代器Map,遍歷得到的是Map.EntryArray,數(shù)組及其它一切符合數(shù)組結(jié)果的對(duì)象

在迭代的同時(shí),我們也可以獲取迭代的狀態(tài)對(duì)象:

trth:each="user,stat:${users}"

tdth:text="${}"Onions/td

tdth:text="${user.age}"2.41/td

/tr

stat對(duì)象包含以下屬性:

index,從0開始的角標(biāo)count,元素的個(gè)數(shù),從1開始size,總元素個(gè)數(shù)current,當(dāng)前遍歷到的元素even/odd,返回是否為奇偶,boolean值first/last,返回是否為第一或最后,boolean值

6、邏輯判斷

有了if和else,我們能實(shí)現(xiàn)一切功能_。

Thymeleaf中使用th:if或者th:unless,兩者的意思恰好相反

spanth:if="${user.age}24"小鮮肉/span

如果表達(dá)式的值為true,則標(biāo)簽會(huì)渲染到頁面,否則不進(jìn)行渲染。

以下情況被認(rèn)定為true:

表達(dá)式值為true表達(dá)式值為非0數(shù)值表達(dá)式值為非0字符表達(dá)式值為字符串,但不是false,no,off表達(dá)式不是布爾、字符串、數(shù)字、字符中的任何一種

其它情況包括null都被認(rèn)定為false

7、分支控制switch

這里要使用兩個(gè)指令:th:switch和th:case

divth:switch="${user.role}"

pth:case="'admin'"用戶是管理員/p

pth:case="'manager'"用戶是經(jīng)理/p

pth:case="*"用戶是別的玩意/p

/div

需要注意的是,一旦有一個(gè)th:case成立,其它的則不再判斷。與java中的switch是一樣的。

另外th:case=*表示默認(rèn),放最后

頁面

8、JS模板

模板引擎不僅可以渲染html,也可以對(duì)JS中的進(jìn)行預(yù)處理。而且為了在純靜態(tài)環(huán)境下可以運(yùn)行,其Thymeleaf代碼可以被注釋起來:

scriptth:inline="javascript"

constuser=/*[[${user}]]*/{};

constage=/*[[${user.age}]]*/20;

console.log(user);

console.log(age)

/script

在script標(biāo)簽中通過th:inline=javascript來聲明這是要特殊處理的js腳本

語法結(jié)構(gòu):

constuser=/*[[Thymeleaf表達(dá)式]]*/"靜態(tài)環(huán)境下的默認(rèn)值";

因?yàn)門hymeleaf被注釋起來,因此即便是靜態(tài)環(huán)境下,js代碼也不會(huì)報(bào)錯(cuò),而是采用表達(dá)式后面跟著的默認(rèn)值。

看看頁面的源碼:

我們的User對(duì)象被直接處理為json格式了,非常方便。

控制臺(tái):

pom.xml依賴

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

groupIdcom.zhf/groupId

artifactIddemoboot4_01/artifactId

version0.0.1-SNAPSHOT/version

namedemoboot4_01/name

descriptiondemoboot4_01/description

properties

java.version1.8/java.version

project.build.sourceEncodingUTF-8/project.build.sourceEncoding

project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding

spring-boot.version2.3.7.RELEASE/spring-boot.version

/properties

dependencies

!--熱部署依賴插件--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-devtools/artifactId

optionaltrue/optional

/dependency

dependency

groupIdorg.mybatis.spring.boot/groupId

artifactIdmybatis-spring-boot-starter/artifactId

version1.3.2/version

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-jdbc/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-thymeleaf/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdmysql/groupId

artifactIdmysql-connector-java/artifactId

scoperuntime/scope

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

exclusions

exclusion

groupIdorg.junit.vintage/groupId

artifactIdjunit-vintage-engine/artifactId

/exclusion

/exclusions

/dependency

/dependencies

dependencyManagement

dependencies

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-dependencies/artifactId

version${spring-boot.version}/version

typepom/type

scopeimport/scope

/dependency

/dependencies

/dependencyManagement

build

plugins

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-compiler-plugin/artifactId

version3.8.1/version

configuration

source1.8/source

target1.8/target

encodingUTF-8/encoding

/configuration

/plugin

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

version2.3.7.RELEASE/version

configuration

mainClasscom.zhf.demoboot4_01.Demoboot401Application/mainClass

/configuration

executions

execution

idrepackage/id

goals

goalrepackage/goal

/goals

/execution

/executions

/plugin

/plugins

/build

/project

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論