上一篇: 詳解SpringBoot整合Freemarker | 帶你讀《SpringBoot實戰教程》之十六 下一篇: SpringBoot整合QuartZ | 帶你讀《SpringBoot實戰教程》之十八 本文來自于千鋒教育在阿裡雲開發者社群學習中心上線課程《SpringBoot實戰教程》,主講人楊紅豔, 點選檢視視訊内容 。
24. SpringBoot整合Thymeleaf
SpringBoot官方是不推薦jsp頁面的,推薦的是模闆引擎。Thymeleaf是其中一種模闆引擎,下面我們看一下SpringBoot如何整合Thymeleaf。
首先要進行依賴,我們把這個依賴放入工程中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
建立templates檔案夾,在下面構模組化闆頁面:

接下來建構一下資料:
啟動之後的結果為:
此時執行結果出錯,我們需要将下列屬性放入全局配置檔案中:
建立application.properties:
#springboot整合thymeleaf
#<!-- 關閉thymeleaf緩存 開發時使用 否則沒有實時畫面-->
spring.thymeleaf.cache=false
## 檢查模闆是否存在,然後再呈現
spring.thymeleaf.check-template-location=true
#Content-Type值
spring.thymeleaf.content-type=text/html
#啟用MVC Thymeleaf視圖分辨率
spring.thymeleaf.enabled=true
## 應該從解決方案中排除的視圖名稱的逗号分隔清單
spring.thymeleaf.excluded-view-names=
#模闆編碼
spring.thymeleaf.mode=LEGACYHTML5
# 在建構URL時預先檢視名稱的字首
spring.thymeleaf.prefix=classpath:/templates/
# 建構URL時附加檢視名稱的字尾.
spring.thymeleaf.suffix=.html
# 鍊中模闆解析器的順序
#spring.thymeleaf.template-resolver-order= o
# 可以解析的視圖名稱的逗号分隔清單
#spring.thymeleaf.view-names=
#thymeleaf end
即聲明thymeleaf使用非嚴格的html。啟動之後通路頁面會報如下錯誤:
上面的異常已經說的很清楚了,需要依賴nekoHTML 1.9.15 or newer的版本。maven依賴如下
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
就取到值了,最後的結果為: