PS:絕對不要為自己想做而不去做的事情找理由。
Thymeleaf 是一個用於 Web 開發的 Java 模板引擎,能夠處理 HTML、XML、JavaScript、CSS 甚至純文本,Spring Boot 推薦使用 Thymeleaf 模板引擎而不是傳統的 JSP 技術,主要內容如下:
- 引入 Thymeleaf
- Thymeleaf 屬性
- Thymeleaf 的使用
- 熱部署
引入 Thymeleaf#
個人覺得 Gradle 相較 Maven 更簡潔,這裡是用 gradle 來構建整個 Web 項目,在 build.gradle 文件中引入 Thymeleaf 依賴如下:
dependencies {
// 引入thymeleaf依賴
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
因為 Thymeleaf 屬於第三方插件,還需在在 build.gradle 文件中指定對應的 classpath ,在 build.gradle 文件中配置如下:
// 第三方的插件需要指定對應的classpath
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.5.RELEASE")
}
}
至此 Thymeleaf 引入到 Web 項目中了,可在導入的庫列表中查看 Thymeleaf 是否正確導入。
Thymeleaf 屬性#
# 啟用模板緩存,默認true
spring.thymeleaf.cache=false
# 在渲染之前檢查模板是否存在
#spring.thymeleaf.check-template=true
# 檢查模板位置是否存在
#spring.thymeleaf.check-template-location=true
# 是否在SpringEL表達式中啟用SpringEL編譯器
#spring.thymeleaf.enable-spring-el-compiler=false
# 是否為Web框架啟用Thymeleaf視圖解析
#spring.thymeleaf.enabled=true
# 模板編碼
#spring.thymeleaf.encoding=UTF-8
# 應該從解決方案中排除的視圖名稱的逗號分隔列表
#spring.thymeleaf.excluded-view-names
# 模板模式
#spring.thymeleaf.mode=HTML
# 構建URL時的前綴
#spring.thymeleaf.prefix=classpath:/templates/
# 構建URL時的後綴
#spring.thymeleaf.suffix=.html
# 逗號分隔的視圖名稱列表,當設置了最大塊大小時,應該是CHUNKED模式中唯一執行的視圖名稱列表
#spring.thymeleaf.reactive.chunked-mode-view-names
# 如果設置了最大塊的大小,應該是FULL模式下的逗號分隔的視圖名稱列表
#spring.thymeleaf.reactive.full-mode-view-names
# 用於寫入響應的數據緩衝區的最大大小,如果設置了模板,則默認情況下將以CHUNKED模式執行
#spring.thymeleaf.reactive.max-chunk-size=0B
# 視圖技術支持的媒體類型
#spring.thymeleaf.reactive.media-types
#Whether hidden form inputs acting as markers for checkboxes should be rendered before the checkbox element itself.
#spring.thymeleaf.render-hidden-markers-before-checkboxes=false
# HTTP響應的Content-Type類型
#spring.thymeleaf.servlet.content-type=text/html
# Thymeleaf應該盡可能的寫入輸出或者緩衝直到模板處理完成
#spring.thymeleaf.servlet.produce-partial-output-while-processing=true
# 模板解析器在鏈中的順序,默認情況下,模板解析器位於鏈中的第一位,從1開始,並且僅在定義了其他TemplateResolver的情況下才能設置
#spring.thymeleaf.template-resolver-order
# 可以解析的視圖名稱的逗號分隔列表
#spring.thymeleaf.view-names
Thymeleaf 的使用#
引入 Thymeleaf 依賴成功之後,在 resources/templates 下面創建模板文件 hello.html 如下:
<!DOCTYPE html>
<!--必須加入thymeleaf的命名空間-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf</title>
</head>
<body>
<p th:text="${name}">Hello World!</p>
</body>
</html>
上述代碼中 Thymeleaf 的標籤都在 Html 標籤中使用,這也是有別於其他模板引擎的地方,${name}
會回去 name 的值在處理模板的時候會將 p
標籤中的內容替換成 name 的值,然後,創建對應的 Controller 如下:
@Controller
public class ThymeleafController {
@RequestMapping("/index")
public String hello(Model model){
model.addAttribute("name","jzman");
return "hello";
}
}
運行之後就可以在訪問如下地址:
http://localhost:8080/index
其運行結果如下:
jzman
熱部署#
在 build.gradle 文件中引入 devtools 如下:
dependencies {
// 熱部署
implementation("org.springframework.boot:spring-boot-devtools:2.0.2.RELEASE")
}
然後,按 Ctrl+Shift+A,找到 Registry 勾選下面這個選項:
最後,在設置 Compiler 中勾選下面這個選項:
配置完成後,為了保證能夠及時更新,應該禁用 Thymeleaf 模板緩存:
spring.thymeleaf.cache=false
運行項目之後,如果項目有所變動,可以使用快捷鍵 Ctrl+F9 快速部署。