javascript
Spring Boot整合Thymeleaf模板引擎
轉(zhuǎn)載自?Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf
Thymeleaf是一款用于渲染XML、XHTML、HTML5內(nèi)容的模板引擎。類似Velocity,FreeMaker模板引擎,它也可以輕易的與Spring MVC等Web框架進(jìn)行集成作為Web應(yīng)用的模板引擎。
Thymeleaf也是Spring Boot首要支持的模板引擎,并且在最新的Spring Boot版本中已經(jīng)不再支持Velocity了。
官網(wǎng):http://www.thymeleaf.org/
引入依賴
需要引入Spring Boot的Thymeleaf啟動(dòng)器依賴。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>引入該依賴后會(huì)自動(dòng)引入web依賴,不需要再單獨(dú)引入web依賴。
自動(dòng)配置說(shuō)明
下面是Thymeleaf的自動(dòng)配置相關(guān)類。
Thymeleaf的自動(dòng)配置類:
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
Thymeleaf的自動(dòng)配置參數(shù)類:
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
查看參數(shù)源碼:
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";默認(rèn)的編碼是:UTF-8
默認(rèn)的類型是:text/html
默認(rèn)的模板文件目錄是:classpath:/templates/
默認(rèn)的模板文件后綴是:.html
這些參數(shù)都可以通過(guò)在application配置文件中指定 spring.thymeleaf.xx進(jìn)行更改,更多可參考該參數(shù)類。
實(shí)戰(zhàn)
知道了自動(dòng)配置的原理,所以我們可以知道怎么做了。
一、在resources目錄下創(chuàng)建templates目錄。
二、在templates目錄下創(chuàng)建.html模板文件。
三、使用模板:
1、模板文件頭部使用 <htmlxmlns:th="http://www.thymeleaf.org">定義。
2、html標(biāo)簽上使用 th:開(kāi)頭標(biāo)識(shí)作為前綴。
3、通過(guò) @{}引入web靜態(tài)文件。
<link rel="stylesheet" th:href="@{/css/jquery.min.css}"/>4、訪問(wèn)數(shù)據(jù)
訪問(wèn)springmvc中的model數(shù)據(jù): ${user.name},訪問(wèn)更多不同對(duì)象的數(shù)據(jù)請(qǐng)點(diǎn)擊參考官方定義。
總結(jié)
以上是生活随笔為你收集整理的Spring Boot整合Thymeleaf模板引擎的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2022年6个最佳演员网站建设者+优秀模
- 下一篇: JVM运行时区域详解