spring boot使用thymeleaf為模板的基本步驟介紹
前言
在開發(fā)過程中,使用模板引擎是很有必要的。jsp已經(jīng)明顯跟不上時代發(fā)展了,freemarker用的夠夠的?換thymeleaf試試吧。
springboot官方推薦的是freemarker和thymeleaf,而thymeleaf相對于freemarker更讓人感覺強大的,是他可以動態(tài)替換標(biāo)簽內(nèi)靜態(tài)內(nèi)容,這樣前端可以安心寫頁面,后臺可以安心擼接口,只需要把變量替換一下即可,這種理念,不知道是VUE抄襲了thymeleaf還是thymeleaf抄襲了VUE,不過無所謂了 ,對于我們廣大碼奴來說,實用就好。
經(jīng)過查閱資料,配置好后,現(xiàn)在將實現(xiàn)的過程分享給大家,下面話不多說了,來一起看看詳細的介紹吧。
壹、pom引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
貳、application.properties添加thymeleaf配置
spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=utf-8 spring.thymeleaf.mode=HTML5 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
叁、編寫html
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>demo</title> </head> <body> <p>這是第一段</p> <p th:text="${textValue}">這是第二段</p> </body> </html>
肆、測試類
package com.mos.easyboot.admin.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("demo") public class DemoController { @RequestMapping("index") public String index(Model model){ String textValue = "上士聞道,僅能行之;中士聞道,若存若亡;下士聞道,大笑之。" + "不笑不足以為道。" + "故建言有之:明道若昧;進道若退;夷道若颣(lei);上德若谷,大白若辱,廣德若不足,建德若偷,質(zhì)真若渝;大方無隅;大器免成;大音希聲;大象無形。" + "道隱無名。" + "夫唯道,善始且善成。"; model.addAttribute("textValue",textValue); return "demo/demo"; } }
伍、頁面效果
陸、數(shù)據(jù)渲染
VUE有個SSR(服務(wù)端渲染)的問題比較頭疼,雖然也有解決方案(見我之前寫的文章《 前后端分離Nuxt.js解決SEO問題 》),但總覺得還是讓適合的技術(shù)做時候的業(yè)務(wù)比較好,而thymeleaf還是相當(dāng)于在服務(wù)端渲染,查看頁面源碼如下:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- Spring Boot + thymeleaf 實現(xiàn)文件上傳下載功能
- Spring boot搭建web應(yīng)用集成thymeleaf模板實現(xiàn)登陸
- spring boot+thymeleaf+bootstrap實現(xiàn)后臺管理系統(tǒng)界面
- springboot+thymeleaf國際化之LocaleResolver接口的示例
- 詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
- 詳解spring Boot 集成 Thymeleaf模板引擎實例
- SpringMVC中使用Thymeleaf模板引擎實例代碼
相關(guān)文章
Kotlin基礎(chǔ)教程之Run,標(biāo)簽Label,函數(shù)Function-Type
這篇文章主要介紹了Kotlin基礎(chǔ)教程之Run,標(biāo)簽Label,函數(shù)Function-Type的相關(guān)資料,需要的朋友可以參考下2017-05-05ThreadPoolExecutor參數(shù)的用法及說明
這篇文章主要介紹了ThreadPoolExecutor參數(shù)的用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java編程隊列數(shù)據(jù)結(jié)構(gòu)代碼示例
這篇文章主要介紹了java編程隊列數(shù)據(jù)結(jié)構(gòu)代碼示例,簡單介紹了隊列的相關(guān)基礎(chǔ)知識,然后通過實例向大家展示其實現(xiàn)方法,具有一定參考價值,需要的朋友可以了解下。2017-11-11java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源)
這篇文章主要介紹了java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08