目錄
一、條件判斷和迭代遍歷
1.1 條件判斷
2.2 迭代遍歷
二、獲取域中的數(shù)據(jù)和URL寫法
2.1 獲取域中的數(shù)據(jù)
2.2 URL寫法
三、相關(guān)配置
一、條件判斷和迭代遍歷
1.1 條件判斷
語(yǔ)法 作用
th:if 條件判斷
準(zhǔn)備數(shù)據(jù)
model.addAttribute("sex","男");
使用實(shí)例
<div>
??? <span th:if="${sex}=='女'">這是女生</span>
??? <span th:if="${sex}=='男'">這是男生</span>
</div>
運(yùn)行結(jié)果:?
當(dāng)然還有th:case也是相當(dāng)于Java中的switch
添加數(shù)據(jù)
model.addAttribute("id",2);
使用實(shí)例
<div th:switch="${id}">
??? <span th:case="1">id為1</span>
??? <span th:case="2">id為2</span>
??? <span th:case="3">id為3</span>
??? <span th:case="*">id為*</span>
</div>
運(yùn)行結(jié)果
2.2 迭代遍歷
編寫實(shí)體類
package com.example.springbootdemo2.pojo;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
準(zhǔn)備數(shù)據(jù)
// 添加List列表集合
User user1 = new User(1,"張三",100);
User user2 = new User(2,"李四",90);
User user3 = new User(3,"王五",60);
User user4 = new User(4,"老六",29);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
model.addAttribute("users",users);?
在頁(yè)面中展示數(shù)據(jù)且配合狀態(tài)變量
thymeleaf將遍歷的狀態(tài)變量封裝到一個(gè)對(duì)象中,通過(guò)該對(duì)象的屬性可以獲取狀態(tài)變量:
狀態(tài)變量 | 含義 |
---|---|
index | 當(dāng)前迭代器的索引,從0開(kāi)始 |
count | 當(dāng)前迭代對(duì)象的計(jì)數(shù),從1開(kāi)始 |
size | 被迭代對(duì)象的長(zhǎng)度 |
odd/even | 布爾值,當(dāng)前循環(huán)是否是偶數(shù)/奇數(shù),從0開(kāi)始 |
first | 布爾值,當(dāng)前循環(huán)的是否是第一條,如果是返回true,否則返回false |
last | 布爾值,當(dāng)前循環(huán)的是否是最后一條,如果是則返回true,否則返回false |
使用實(shí)例
<table border="1">
??? <tr>
??????? <th>id</th>
??????? <th>姓名</th>
??????? <th>年齡</th>
??????? <th>當(dāng)前迭代器的索引,從0開(kāi)始</th>
??????? <th>當(dāng)前迭代對(duì)象的計(jì)數(shù),從1開(kāi)始</th>
??????? <th>被迭代對(duì)象的長(zhǎng)度</th>
??????? <th>布爾值,當(dāng)前循環(huán)是否是偶數(shù),從0開(kāi)始</th>
??????? <th>布爾值,當(dāng)前循環(huán)是否是奇數(shù),從0開(kāi)始</th>
??????? <th>布爾值,當(dāng)前循環(huán)的是否是第一條,如果是返回true,否則返回false</th>
??????? <th>布爾值,當(dāng)前循環(huán)的是否是最后一條,如果是則返回true,否則返回false</th>
??? </tr>
??? <tr th:each="user,status: ${users}">
??????? <td th:text="${user.getId()}"></td>
??????? <td th:text="${user.getName()}"></td>
??????? <td th:text="${user.getAge()}"></td>
??????? <td th:text="${status.index}"></td>
??????? <td th:text="${status.count}"></td>
??????? <td th:text="${status.size}"></td>
??????? <td th:text="${status.odd}"></td>
??????? <td th:text="${status.even}"></td>
??????? <td th:text="${status.first}"></td>
??????? <td th:text="${status.last}"></td>
??? </tr>
</table>
運(yùn)行結(jié)果:?
遍歷Map
準(zhǔn)備數(shù)據(jù)
// 添加map集合數(shù)據(jù)
Map<String,User> userMap = new HashMap<>();
userMap.put("user1",user1);
userMap.put("user2",user2);
userMap.put("user3",user3);
userMap.put("user4",user4);
model.addAttribute("userMap",userMap);
使用實(shí)例?
<table>
??? <tr>
??????? <th>ID</th>
??????? <th>Name</th>
??????? <th>Age</th>
??????? <th>Key</th>
??? </tr>
??? <tr th:each="m : ${userMap}">
??????? <td th:text="${m.value.getId()}"></td>
??????? <td th:text="${m.value.getName()}"></td>
??????? <td th:text="${m.value.getAge()}"></td>
??????? <td th:text="${m.key}"></td>
??? </tr>
</table>
運(yùn)行結(jié)果:?
二、獲取域中的數(shù)據(jù)和URL寫法
2.1 獲取域中的數(shù)據(jù)
thymeleaf也可以獲取request,session,application域中的數(shù)據(jù),方法如下:
準(zhǔn)備數(shù)據(jù)
// 往request域設(shè)置數(shù)據(jù)
req.setAttribute("req","request");
// 往response域設(shè)置數(shù)據(jù)
session.setAttribute("session","session");
// 往application域設(shè)置數(shù)據(jù)
session.getServletContext().setAttribute("app","application");
使用實(shí)例
request域獲取方式1: <span th:text="${#request.getAttribute('req')}"></span>
request域獲取方式2: <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<hr>
session域獲取方式1: <span th:text="${#session.getAttribute('session')}"></span>
session域獲取方式2: <span th:text="${#httpSession.getAttribute('session')}"></span>
<hr>
application域獲取方式1: <span th:text="${application.app}"></span>
application域獲取方式2: <span th:text="${#servletContext.getAttribute('app')}"></span>
<hr>
運(yùn)行結(jié)果:
2.2 URL寫法
在Thymeleaf中路徑的寫法為 @{路徑},同樣也可以在路徑中添加參數(shù),使用RestFul樣式URL。
準(zhǔn)備數(shù)據(jù)
model.addAttribute("id",100);
model.addAttribute("name","lyl");
添加跳轉(zhuǎn)路徑
@GetMapping("/show2")
@ResponseBody
public String showPage2(String id,String name){
??? return id+":"+name;
}// @RestFul風(fēng)格傳遞參數(shù)
@GetMapping("/show3/{id}/{name}")
@ResponseBody
public String showPage3(@PathVariable String id,@PathVariable String name){
??? return id + ":" + name;
}
使用實(shí)例
<a th:href="@{https://www.baidu.com}">百度</a>
<a th:href="@{show2?id=1&name='lyl'}">靜態(tài)參數(shù)一</a>
<a th:href="@{show2(id=1,name='hqx')}">靜態(tài)參數(shù)二</a>
<a th:href="@{'show2?id='+${id}+'&name='+${name}}">動(dòng)態(tài)參數(shù)一</a>
<a th:href="@{show2(id=${id},name=${name})}">動(dòng)態(tài)參數(shù)二</a>
<a th:href="@{show3/{id}/{name}(id=${id},name=${name})}">RestFul風(fēng)格傳遞參數(shù)</a><hr>
運(yùn)行結(jié)果
三、相關(guān)配置
在Springboot配置文件中可以進(jìn)行Thymeleaf相關(guān)配置文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-715939.html
配置項(xiàng) | 含義 |
---|---|
spring.thymeleaf.prefix | 視圖前綴 |
spring.thymeleaf.suffix | 視圖后綴 |
spring.thymeleaf.encoding | 編碼格式 |
spring.thymeleaf.servlet.content-type | 響應(yīng)類型 |
spring.thymeleaf.cache=false | 頁(yè)面緩存,配置為false則不啟用頁(yè)面緩存,方便測(cè)試 |
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-715939.html
到了這里,關(guān)于SpringBoot自帶模板引擎Thymeleaf使用詳解②的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!