1.thymeleaf簡介
thymeleaf是個xml/xhtml/html5模板引擎,可以用于web與非web應(yīng)用
thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模,thymeleaf的可擴(kuò)展性也非常棒。你可以使用它定義自己的模板屬性集合,這樣就可以計(jì)算自定義表達(dá)式并使用自定義邏輯,thymeleaf還可以作為模板引擎框架。
2.引入thymeleaf
引入依賴
在maven(pom.xml)中直接引入:
1
2
3
4
5
6
7
8
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> |
配置thymeleaf
在application.yml配置thymeleaf
1
2
3
4
5
6
7
8
9
10
11
12
|
server: port: 8000 spring: thymeleaf: cache: false # 關(guān)閉頁面緩存 encoding: utf- 8 # 模板編碼 prefix: classpath:/templates/ # 頁面映射路徑 suffix: .html # 試圖后的后綴 mode: html5 # 模板模式 # 其他具體配置可參考o(jì)rg.springframework.boot.autoconfigure.thymeleaf.thymeleafproperties # 上面的配置實(shí)際上就是注入該類的屬性值 |
demo示例
創(chuàng)建indexcontroller
1
2
3
4
5
6
7
8
9
|
@controller public class indexcontroller { // 返回視圖頁面 @requestmapping ( "index" ) public string index(){ return "index" ; } } |
創(chuàng)建index.html
1
2
3
4
5
6
7
8
9
10
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body> hello thymeleaf! </body> </html> |
創(chuàng)建testcontroller
1
2
3
4
5
6
7
8
9
|
@restcontroller public class testcontroller { // 返回整個頁面 @requestmapping ( "/test" ) public modelandview test(){ return new modelandview( "test" ); } } |
創(chuàng)建test.html
1
2
3
4
5
6
7
8
9
10
11
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body> hello thymeleaf! </br> by: modelandview </body> </html> |
3.測試結(jié)果
4.thymeleaf基礎(chǔ)語法及使用
1.引入標(biāo)簽
html標(biāo)簽里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*這樣的語法
2.引入url
@{...}
例如:
<a th:href="@{http://www.baidu.com}" rel="external nofollow" >絕對路徑</a> 是訪問絕對路徑下的url, <a th:href="@{/}" rel="external nofollow" >相對路徑</a> 是訪問相對路徑下的url。
<a th:href="@{css/bootstrap.min.css}" rel="external nofollow" >是引入默認(rèn)的static下的css文件夾下的bootstrap文件,類似的標(biāo)簽有: th:href 和 th:src
3.獲取變量
通過${}取值,對于javabean的話,使用變量名.屬性名獲取
4.字符串替換
<span th:text="'welcome to our application, ' + ${user.name} + '!'"></span>
或者
<span th:text="|welcome to our application, ${user.name}!|"></span>
注意:|…|中只能包含變量表達(dá)式${…},不能包含其他常量、條件表達(dá)式等
5.運(yùn)算符
在表達(dá)式中可以使用各類算術(shù)運(yùn)算符
例如 (+, -, *, /, %)
例如:th:with="iseven=(${stat.number} % 1 == 0)"
邏輯運(yùn)算符 (>, <, <=,>=,==,!=)
需要注意的是使用<,>的時(shí)候需要轉(zhuǎn)義
1
2
|
th: if = "${stat.number} > 1" th:text= "'execution mode is ' + ( (${execmode} == 'dev')? 'development' : 'production')" |
6.條件
if/unless th:if是該標(biāo)簽在滿足條件的時(shí)候才會顯示,unless是不成立時(shí)候才顯示
1
|
<a th:href= "@{/login}" rel= "external nofollow" th:unless=${user.number != null }>login</a> |
switch thymeleaf支持switch結(jié)構(gòu),默認(rèn)屬性(default)用*表示
1
2
3
4
5
|
<div th: switch = "${user.role}" > <p th: case = "'admin'" >user is an administrator</p> <p th: case = "#{roles.manager}" >user is a manager</p> <p th: case = "*" >user is some other thing</p> </div> |
7.循環(huán)
1
2
3
4
5
|
<tr th:each= "prod : ${prods}" > <td th:text= "${prod.name}" >onions</td> <td th:text= "${prod.price}" > 2.41 </td> <td th:text= "${prod.instock}? #{true} : #{false}" >yes</td> </tr> |
8.utilities
內(nèi)置在context中,可以直接通過#訪問
#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps
…
5.小結(jié)
本文講述了如何在spring boot中引入模板引擎thymeleaf以及thymeleaf基礎(chǔ)語法和實(shí)際使用
本文github地址:https://github.com/ishuibo/springall
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。