引入
最早開發的時候,展示頁面我們都是使用HTML完成我們的代碼撰寫;但是我們的顯示頁面一定是需要動態變化的,之后就引入了Jsp技術,用來進行資料的顯示及互動,但是Jsp是以war包進行部署,但是之后想用jar包的方式打包,這種方式就會很麻煩,所以就有了模板引擎技術 ,模板引擎有很多,比如jsp,freemarker,thymeleaf等,我們用thymeleaf來舉例
參考地址
官網地址:https://www.thymeleaf.org/
github地址:https://github.com/thymeleaf/thymeleaf
中文網站:https://raledong.gitbooks.io/using-thymeleaf/content/
使用
先引入依賴,我用SpringBoot的starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在springboot中有專門的thymeleaf配置類:ThymeleafProperties
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
/**
* Whether to check that the template exists before rendering it.
*/
private boolean checkTemplate = true;
..................................
@Controller
public class RequestController {
@GetMapping("/request")
public String request(Model model){
model.addAttribute("msg","name");
return "show";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
標準運算式語法
簡單運算式
| 運算式名字 | 語法 | 用途 |
|---|---|---|
| 變數取值 | ${...} | 獲取請求域、session域、物件等值 |
| 選擇變數 | *{...} | 獲取背景關系物件值 |
| 訊息運算式 | #{...} | 獲取國際化等值 |
| 鏈接URL | @{...} | 生成鏈接 |
| 片段運算式 | ~{...} | 同jsp:include 作用,引入公共頁面片段 |
常量
- 文本常量 :
'one text','Another one!',… - 數字常量 :
0,34,3.0,12.3,… - 布爾常量 :
true,false - 空常量 :
null - 常符號 :
one,sometext,main,…
文本操作
- 字串連接 :
+ - 常量替換 :
|The name is ${name}|
算數操作
- 二元運算子:
+,-,*,/,%
布爾操作
- 布爾運算子 :
and,or - 布爾取反(一元運算子):
!,not
比較運算
- 比較符號:
>,<,>=,<=(gt,lt,ge,le) - 相等符號:
==,!=(eq,ne)
條件運算子
- If-then:
(if) ? (then) - If-then-else:
(if) ? (then) : (else) - 默認值 Default:
(value) ?: (defaultvalue)
特殊符號
- 無運算子 :
_
th的常用屬性值
th:text/ th:utext :設定當前元素的文本內容,兩者的區別在于前者不會轉義html標簽,后者會,優先級不高:order=7
th:value/ th:src/ th:href:設定當前元素的value值,優先級不高:order=6
th:each:遍歷回圈元素,和th:text或th:value一起使用,注意該屬性修飾的標簽位置,詳細往后看,優先級很高:order=2
th:if:條件判斷,類似的還有th:unless,th:switch,th:case,優先級較高:order=3
th:insert/ th:include/ th:replace:代碼塊引入,三者的區別較大,常用于公共代碼塊提取的場景,優先級最高:order=1
th:fragment:定義代碼塊,方便被th:insert參考,優先級最低:order=8
th:object:宣告變數,一般和*{}一起配合使用,達到偷懶的效果,優先級一般:order=4
th:attr/ th:attrappend/ th:attrprepend:修改任意屬性優先級一般:order=5
所有h5兼容的標簽寫法:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes
關于屬性的優先級

thymeleaf內置方法
此外thymeleaf有很多內置方法,與Java的API類似!
-
strings:字串格式化方法,常用的Java方法它都有,比如:equals,length,trim,toUpperCase,indexOf等
-
numbers:數值格式化方法,常用的方法有:formatDecimal等
-
bools:布爾方法,常用的方法有:isTrue,isFalse等
-
arrays:陣列方法,常用的方法有:toArray,length,isEmpty,contains,containsAll等
-
lists,sets:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等
-
maps:物件方法,常用的方法有:size,isEmpty,containsKey,containsValue等
-
dates:日期方法,常用的方法有:format,year,month,hour,createNow等
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241716.html
標籤:Java
