??Spring Boot支持FreeMarker漓帚、Groovy、Thymeleaf和Mustache四種模板解析引擎午磁,其中官方推薦使用Thymeleaf尝抖。
1. 引入依賴
<!-- 引入thymeleaf依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在Spring Boot中,默認(rèn)的html頁(yè)面地址為src/main/resources/templates迅皇,默認(rèn)的靜態(tài)資源地址為src/main/resources/static昧辽。
2. 配置文件支持屬性
#THYMELEAF(ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true #開(kāi)啟模板緩存(默認(rèn)值:true)
spring.thymeleaf.check-template=true #是否在渲染之前檢查模板是否存在。
spring.thymeleaf.check-template-location=true #檢查模板位置是否正確(默認(rèn)值:true)
spring.thymeleaf.content-type=text/html #Content-Type的值(默認(rèn)值:text/html)
spring.thymeleaf.enabled=true #是否為Web框架啟用Thymeleaf視圖解析(默認(rèn)值:true)
spring.thymeleaf.enable-spring-el-compiler = false #在SpringEL表達(dá)式中啟用SpringEL編譯器登颓。
spring.thymeleaf.encoding=UTF-8 #模板編碼
spring.thymeleaf.excluded-view-names= #要被排除在解析之外的視圖名稱列表搅荞,用逗號(hào)分隔
spring.thymeleaf.mode=HTML5 #要運(yùn)用于模板之上的模板模式。另請(qǐng)參閱Thymeleaf的TemplateMode枚舉(默認(rèn)值:HTML5)
spring.thymeleaf.prefix=classpath:/templates/ #在構(gòu)建URL時(shí)添加到視圖名稱前的前綴(默認(rèn)值:classpath:/templates/)
spring.thymeleaf.reactive.chunked-mode-view-names = #逗號(hào)分隔的視圖名稱列表(允許的模式)框咙,當(dāng)設(shè)置最大塊大小時(shí)咕痛,應(yīng)該是CHUNKED模式中唯一執(zhí)行的視圖名稱列表。
spring.thymeleaf.reactive.full-mode-view-names =#即使設(shè)置了最大塊大小喇嘱,也應(yīng)該在FULL模式下執(zhí)行逗號(hào)分隔的視圖名稱列表(允許的模式)茉贡。
spring.thymeleaf.reactive.max-chunk-size = 0 #用于寫(xiě)入響應(yīng)的數(shù)據(jù)緩沖區(qū)的最大大小(以字節(jié)為單位)者铜。
spring.thymeleaf.reactive.media-types = #視圖技術(shù)支持的媒體類型腔丧。
spring.thymeleaf.servlet.content-type = text / html #寫(xiě)入HTTP響應(yīng)的Content-Type值。
spring.thymeleaf.suffix=.html #在構(gòu)建URL時(shí)添加到視圖名稱后的后綴(默認(rèn)值:.html)
spring.thymeleaf.template-resolver-order= 1 #Thymeleaf模板解析器在解析器鏈中的順序作烟。默認(rèn)情況下愉粤,它排第一位。順序從1開(kāi)始拿撩,只有在定義了額外的TemplateResolver Bean時(shí)才需要設(shè)置這個(gè)屬性衣厘。
spring.thymeleaf.view-names= #可解析的視圖名稱列表,用逗號(hào)分隔
一般我們將spring.thymeleaf.cache設(shè)置為false压恒,其他保持默認(rèn)值即可头滔。
3. 示例
。涎显。坤检。so easy距辆!