Leetcode 287. 尋找重複數 中等 陣列 快慢指標
Thymeleaf作為後端頁面開發模板,避免了前後端分離帶來的跨域等問題,對於後臺管理等簡單頁面可以快速進行開發迭代。 學習Thymeleaf,首先了解一下如何將Thymeleaf整合到SpringBoot專案中使用。
1. 建立SpringBoot專案
1.1 選擇起步依賴
在建立SpringBoot專案時,官方提供了很多的起步依賴,其中就有Thymeleaf相關的啟動器,在Template Engines模板引擎下勾選Thymeleaf啟動器,由於是Web頁面的開發,所以還需要引入Web模組啟動器。
勾選後點選下一步並完成專案建立。
1.2 手動新增依賴
如果在專案建立時沒有選擇相關的Thymeleaf啟動器,則可以在maven的座標配置pom.xml檔案中新增thymeleaf的依賴資訊。
<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的SpringBoot專案,結構上與普通專案一樣,但是在resources資料夾下多了一個templates資料夾,這個資料夾就是用於存放Thymeleaf模板檔案。
2. Thymeleaf配置
2.1 SpringBoot自動配置Thymeleaf
SpringBoot框架中提供了對Thymeleaf模板引擎的自動載入配置,在SpringBoot的自動配置類包中,有一個org.springframework.boot.autoconfigure.thymeleaf
包,定義的ThymeleafProperties類就是thymeleaf的自動配置類,其中指定了thymeleaf框架的一些預設屬性,比如模板的預設路徑字首classpath:/templates/
.html
。
ThymeleafProperties類的部分定義內容如下:
//自動配置類ThymeleafProperties
@ConfigurationProperties( prefix = "spring.thymeleaf" )
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
...
}
複製程式碼
可以看出,配置類讀取的是SpringBoot配置檔案中以spring.thymeleaf
作為字首的配置內容,因此,如果需要自定義thymeleaf的相關配置,只需要在application.properties
檔案中指定即可。
2.2 自定義配置項
自動配置類ThymeleafProperties初始化時會讀取application.properties
檔案中配置的內容,配置檔案中可以定義的thymeleaf配置項有:
# thymeleaf模板檔案字首,可以自定義資料夾如classpath:/templates/temp
spring.thymeleaf.prefix=classpath:/templates/
# thymeleaf模板檔案字尾
spring.thymeleaf.suffix=.html
# 檢視模板型別
spring.thymeleaf.mode=HTML
# 預設檢視編碼格式
spring.thymeleaf.encoding=UTF-8
# 響應型別
spring.thymeleaf.servlet.content-type=text/html
# 配置頁面快取,thymeleaf預設開啟快取,頁面不能及時重新整理,需要關閉
spring.thymeleaf.cache=false
複製程式碼
通過自定義的配置相關屬性的值,在實際使用時可以更好的控制對thymeleaf模板引擎的使用。
3. Thymeleaf頁面效果
建立了專案,添加了配置,下面就來看一下thymeleaf帶來的頁面效果。
3.1 定義HTML檔案
使用thymeleaf模板引擎展示頁面,首先要建立一個html檔案,並按照thymeleaf語法編碼與介面資料繫結。
- 定義html檔案時,要對其中的
<html>
標籤使用xmlns:th="http://www.thymeleaf.org"
宣告,這樣在當前html頁面中才可以使用thymeleaf語法。 - 對於服務介面繫結返回的資料集合,可以使用
th:each="user : ${userEntityList}"
遍歷對應值 - 對於每個屬性的值,使用
th:text="${user.getName()}"
來獲取展示
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>性別</th>
<th>電話</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userEntityList}">
<td th:text="${user.getName()}">name</td>
<td th:text="${user.getGender()}">male</td>
<td th:text="${user.getPhone()}">13812341234</td>
</tr>
</tbody>
</table>
</body>
</html>
複製程式碼
3.2 定義服務介面
定義服務介面來向thymeleaf模板傳遞資料,介面定義時需要注意:
- 使用@Controller註解而不是@RestController,因此@RestController會將結果解析後直接展示字串內容,而不能根據字串獲取對應的模板名稱
- 服務介面需要加入Model物件作為引數,並使用Model物件來繫結需要傳遞的資料,以便在thymeleaf中使用
@Controller
public class ThymeleafController {
@RequestMapping("/index")
public String getIndex(Model model){
List<UserEntity> userList = new ArrayList<>();
UserEntity user = new UserEntity("tom","female", "17788996600");
userList.add(user);
model.addAttribute(userList);
return "index";
}
}
3.3 頁面展示
如此這般之後,可以執行專案,並請求定義的服務介面,此時會根據介面將資料加入Model中,並根據介面的返回結果找到對應的thymeleaf模板檔案,在模板檔案中將資料渲染,最終展示在頁面中。
轉載:https://juejin.cn/post/7030820165042307108