這篇文章主要介紹SpringBoot怎么引入Thymeleaf方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元益陽(yáng)做網(wǎng)站,已為上家服務(wù),為益陽(yáng)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
1、Thymeleaf簡(jiǎn)介
Thymeleaf是個(gè)XML/XHTML/HTML5模板引擎,可以用于Web與非Web應(yīng)用
Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模,Thymeleaf的可擴(kuò)展性也非常棒。你可以使用它定義自己的模板屬性集合,這樣就可以計(jì)算自定義表達(dá)式并使用自定義邏輯,Thymeleaf還可以作為模板引擎框架。
2、引入Thymeleaf
引入依賴
在maven(pom.xml)中直接引入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
配置Thymeleaf
在application.yml配置Thymeleaf
server: port: 8000 spring: thymeleaf: cache: false # 關(guān)閉頁(yè)面緩存 encoding: UTF-8 # 模板編碼 prefix: classpath:/templates/ # 頁(yè)面映射路徑 suffix: .html # 試圖后的后綴 mode: HTML5 # 模板模式 # 其他具體配置可參考o(jì)rg.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties # 上面的配置實(shí)際上就是注入該類的屬性值
demo示例
創(chuàng)建IndexController
@Controller public class IndexController { // 返回視圖頁(yè)面 @RequestMapping("index") public String index(){ return "index"; } }
創(chuàng)建index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello Thymeleaf! </body> </html>
創(chuàng)建TestController
@RestController public class TestController { // 返回整個(gè)頁(yè)面 @RequestMapping("/test") public ModelAndView test(){ return new ModelAndView("test"); } }
創(chuàng)建test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello Thymeleaf! </br> By: ModelAndView </body> </html>
3、測(cè)試結(jié)果
4、Thymeleaf基礎(chǔ)語(yǔ)法及使用
引入標(biāo)簽
html標(biāo)簽里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*這樣的語(yǔ)法
引入U(xiǎn)RL
@{...}
例如:
<a th:href="@{http://www.baidu.com}">絕對(duì)路徑</a> 是訪問(wèn)絕對(duì)路徑下的URL, <a th:href="@{/}">相對(duì)路徑</a> 是訪問(wèn)相對(duì)路徑下的URL。 <a th:href="@{css/bootstrap.min.css}">是引入默認(rèn)的static下的css文件夾下的bootstrap文件,類似的標(biāo)簽有: th:href 和 th:src
3.獲取變量
通過(guò)${}取值,對(duì)于JavaBean的話,使用變量名.屬性名獲取
4.字符串替換
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"></span> 或者 <span th:text="|Welcome to our application, ${user.name}!|"></span> 注意:|…|中只能包含變量表達(dá)式${…},不能包含其他常量、條件表達(dá)式等
5.運(yùn)算符
在表達(dá)式中可以使用各類算術(shù)運(yùn)算符
例如 (+, -, *, /, %)
例如:th:with="isEven=(${stat.number} % 1 == 0)"
邏輯運(yùn)算符 (>, <, <=,>=,==,!=)
需要注意的是使用<,>的時(shí)候需要轉(zhuǎn)義
th:if="${stat.number} > 1" th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"
6.條件
if/unless th:if是該標(biāo)簽在滿足條件的時(shí)候才會(huì)顯示,unless是不成立時(shí)候才顯示
<a th:href="@{/login}" th:unless=${user.number != null}>Login</a>
switch thymeleaf支持switch結(jié)構(gòu),默認(rèn)屬性(default)用*表示
<p th:switch="${user.role}"> <p th:case="'admin'">User is an administrator</p> <p th:case="#{roles.manager}">User is a manager</p> <p th:case="*">User is some other thing</p> </p>
7.循環(huán)
<tr th:each="prod : ${prods}"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr>
8.Utilities
內(nèi)置在Context中,可以直接通過(guò)#訪問(wèn) #dates #calendars #numbers #strings arrays lists sets maps …
以上是SpringBoot怎么引入Thymeleaf方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:SpringBoot怎么引入Thymeleaf方法
本文地址:http://chinadenli.net/article10/pehgdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)