Spring Boot 整合 Thymeleaf

2020-12-13 村雨遙

目錄

1. 什麼是 Thymeleaf2. 整合過程2.1 添加 Thymeleaf 依賴2.2 編寫實體類和 Controller2.3 創建 Thymeleaf 模板2.4 測試3. 注意事項

1. 什麼是 Thymeleaf

Thymeleaf 是新一代的 Java 模板引擎,類似於 Velocity、FreeMarker 等傳統引擎,其語言和 HTML 很接近,而且擴展性更高;Thymeleaf 的主要目的是將優雅的模板引入開發工作流程中,並將 HTML 在瀏覽器中正確顯示。同時能夠作為靜態引擎,讓開發成員之間更方便協作開發;Spring Boot 官方推薦使用模板,而且 Spring Boot 也為 Thymeleaf 提供了完整的自動化 配置解決方案;Thymeleaf 使用教程請戳 Tutorial: Using Thymeleaf[1],配合 Spring 使用的教程請戳 Tutorial: Thymeleaf + Spring[2]。2. 整合過程

2.1 添加 Thymeleaf 依賴

添加 Thymeleaf 依賴有兩種方式:

在新建項目時添加,在 Templeate Engines 中勾選 Thymeleaf;

對於未添加 Thymeleaf 依賴的項目,直接在 pom.xml 中手動添加依賴即可;<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>2.2 編寫實體類和 Controller

新建實體類 Userpackage com.cunyu.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.stereotype.Component;/** * @author : cunyu * @version : 1.0 * @className : Author * @date : 2020/7/29 16:20 * @description : User 實體類 */@Component@Data@AllArgsConstructor@NoArgsConstructorpublicclassUser{privateint age;private String name;private String email;}編寫 Controllerpackage com.cunyu.controller;import com.cunyu.pojo.User;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;/** * @author : cunyu * @version : 1.0 * @className : UserController * @date : 2020/7/29 16:22 * @description : UserController */@ControllerpublicclassUserController{// 訪問 ip:port/index@GetMapping("/index")public ModelAndView index(){ ModelAndView modelAndView = new ModelAndView();// 設置跳轉的視圖 modelAndView.setViewName("index"); modelAndView.addObject("title", "Thymeleaf 使用"); modelAndView.addObject("desc", "Spring Boot 整合 Thymeleaf"); User author = new User(25, "村雨遙", "747731461@qq.com"); modelAndView.addObject("author", author);return modelAndView; }}2.3 創建 Thymeleaf 模板

第 2.3 中,設置了跳轉的視圖為 index,所以我們需要在 src/main/resources/templates 中創建 index.html;

<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><titleth:text="${title}"></title></head><body><h1th:text="${desc}"th:align="center"></h1><h2th:align="center">=====作者信息=====</h2><pth:text="${author?.name}"></p><pth:text="${author?.age}"></p><pth:text="${author?.email}"></p></body></html>2.4 測試

啟動項目,然後在瀏覽器中訪問 http://localhost:8080/index,如果出現下圖中的信息,說明整合成功。

3. 注意事項

為了方便使用,我們在使用 Thymeleaf 模板時,可以添加一些自己的配置;

# thymelea模板配置# 設置模板文件存放位置spring.thymeleaf.prefix=classpath:/templates/# 設置模板後綴spring.thymeleaf.suffix=.html# 語法嚴格限制spring.thymeleaf.mode=HTML5# 編碼格式spring.thymeleaf.encoding=UTF-8# 熱部署,每次修改靜態頁面都不用重啟就可以生效,默認為 truespring.thymeleaf.cache=false參考資料

[1]Tutorial: Using Thymeleaf: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

[2]Tutorial: Thymeleaf + Spring: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

相關焦點