thymeleaf系列二簡單表達(dá)式變量消息literals文本算術(shù)比較和條件表達(dá)式_第1頁
thymeleaf系列二簡單表達(dá)式變量消息literals文本算術(shù)比較和條件表達(dá)式_第2頁
thymeleaf系列二簡單表達(dá)式變量消息literals文本算術(shù)比較和條件表達(dá)式_第3頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、thymeleaf 系列二簡單表達(dá)式變量消息 literals 文本算術(shù)比較和條件表達(dá)式1. 概述本節(jié)主要介紹 thymeleaf 的語法:簡單表達(dá)式。包括以下內(nèi)容:- 變量表達(dá)式: Variable Expressions- 消息表達(dá)式: Message Expressions- Literals- 文本操作符:Text operations- 算術(shù)表達(dá)式: Arithmetic operations- 比較操作符: Comparisons and equality- 條件操作符: Conditional operators2. 例子2.1 公共類Userpublic class User

2、private String name;private boolean isAdmin;private String other;private int age;/ set/get 略12345671234567Familypublic class Family private User father;private List childList;/ set/get 略1234512345ExpressionsCtl : Control 類此類初始化測試類,當(dāng)訪問此URL ,并轉(zhuǎn)到expressions/simple.html 。ControllerRequestMapping("/

3、expressions")public class ExpressionsCtl /* 簡單表達(dá)式*/RequestMapping("/simple")public String simpleExpressions(ModelMap map)/ 變量表達(dá)式: Variable Expressions User user = new User("simple_name"); user.setAge(new Random().nextInt(100); map.put("user", user);return "exp

4、ressions/simple".12345678910111213141516171234567891011121314151617下面的代碼都在此simple.html 頁面中。2.2 變量表達(dá)式 (Variable Expressions)實現(xiàn)功能:簡單的表達(dá)式變量值和字符串拼接=變量表達(dá)式 (Variable Expressions)=$ -> 'The name of the user is ' + $ -> 123456123456輸出 : “ >”的左邊是語法,右邊是對應(yīng)的輸出=變量表達(dá)式 (Vari

5、able Expressions)=$ -> simple_name'The name of the user is ' + $ -> The name of the user is simple_name 123412342.3 消息表達(dá)式:Message Expressions實現(xiàn)功能:直接從屬性文件中獲取值從屬性文件中獲取值,并替換占位符#home.welcome ->Welcome to our grocery store!#home.welcome.replace($) ->12345612

6、3456輸出 : “ >”的左邊是語法,右邊是對應(yīng)的輸出=消息表達(dá)式: Message Expressions= #home.welcome -> welcome thymeleaf #home.welcome.replace($) -> welcomethymeleaf, simple_name! 1231232.4 Literals實現(xiàn)功能:文本Text literals數(shù)字Number literals布爾值Null 值= Literals=Now you are looking at a template file. 僅僅輸出數(shù)字 th:text=&

7、quot;2013" -> 1492.數(shù)字計算th:text="2013 + 2" -> 1494.$user.isAdmin() = false ->false$user.other = null->null1234567891011121314151612345678910111213141516輸出 : “ >”的左邊是語法,右邊是對應(yīng)的輸出= Literals=Now you are looking at a working web application.僅僅輸出數(shù)字th:text="2013" ->

8、; 2013.數(shù)字計算th:text="2013 + 2" -> 2015.$user.isAdmin() = false -> false$user.other = null -> null 1234561234562.5 文本操作符:Text operations實現(xiàn)功能:+:字符串拼接字體串簡化字符中拼接操作:Literal substitutions (使用” |”包圍字符串,不需要對字符串使用”)'The name of the user is ' + $ + '_' + $user.age -&

9、gt;|Welcome to our application, $!| ->等價于這條語句:'Welcome to our application, ' + $ + '!' ->12345671234567輸出 : “ >”的左邊是語法,右邊是對應(yīng)的輸出= 文本操作符: Text operations: ='The name of the user is ' + $ + '_' + $user.age -> The name of the user is

10、 simple_name_52|Welcome to our application, $!| -> Welcome to our application, simple_name!等價于這條語句:'Welcome to our application, ' + $ + '!' -> Welcome to our application, simple_name! 12345123452.6 算術(shù)表達(dá)式: Arithmetic operations 實現(xiàn)功能:二進(jìn)制運算符 : Binary operators: +

11、, -, *, /, %Boolean operations: true,false, !, notBinary operators: and, or$user.age % 2 = 0 ->true ->!($user.age % 2 = 0 -> ($user.age % 2 = 0) and true ->1234567891012345678910輸出 : “ >”的左邊是語法,右邊是對應(yīng)的輸出= 算術(shù)表達(dá)式: Arithmetic operations = $user.age % 2 = 0 -> truetrue -> true!($user

12、.age % 2 = 0 -> false($user.age % 2 = 0) and true -> true1234512345 2.7 比較操作符: Comparisons and equality實現(xiàn)功能:比較符號 Comparators: >, =,Equality operators: =, != (eq, ne)= 比較操作符: Comparisons and equality = , =,$user.age > 18 ->大人$user.age != 18 ->大人_no_equality123456123456輸出 : “ >

13、;”的左邊是語法,右邊是對應(yīng)的輸出=比較操作符: Comparisonsand equality =$user.age > 18 ->大人$user.age != 18 -> 大人 _no_equality 123123 2.8 條件操作符: Conditional operators實現(xiàn)功能:If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)如果 null 值,則使用? :后面的值 : Default: (value) ?:(defaultvalue)$user.age%2=0 ? 'even' ->$user.age%2=0 ? 'even' :'odd' ->$user.age ?:'18' ->12345671234567輸出 :

溫馨提示

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

最新文檔

評論

0/150

提交評論