這篇文章給大家分享的是有關(guān)spring boot如何使用thymeleaf為模板的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
在衡水等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)整合營(yíng)銷(xiāo)推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,衡水網(wǎng)站建設(shè)費(fèi)用合理。
前言
在開(kāi)發(fā)過(guò)程中,使用模板引擎是很有必要的。jsp已經(jīng)明顯跟不上時(shí)代發(fā)展了,freemarker用的夠夠的?換thymeleaf試試吧。
springboot官方推薦的是freemarker和thymeleaf,而thymeleaf相對(duì)于freemarker更讓人感覺(jué)強(qiáng)大的,是他可以動(dòng)態(tài)替換標(biāo)簽內(nèi)靜態(tài)內(nèi)容,這樣前端可以安心寫(xiě)頁(yè)面,后臺(tái)可以安心擼接口,只需要把變量替換一下即可,這種理念,不知道是VUE抄襲了thymeleaf還是thymeleaf抄襲了VUE,不過(guò)無(wú)所謂了 ,對(duì)于我們廣大碼奴來(lái)說(shuō),實(shí)用就好。
經(jīng)過(guò)查閱資料,配置好后,現(xiàn)在將實(shí)現(xiàn)的過(guò)程分享給大家,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
壹、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
叁、編寫(xiě)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>
肆、測(cè)試類(lèi)
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 = "上士聞道,僅能行之;中士聞道,若存若亡;下士聞道,大笑之。" + "不笑不足以為道。" + "故建言有之:明道若昧;進(jìn)道若退;夷道若颣(lei);上德若谷,大白若辱,廣德若不足,建德若偷,質(zhì)真若渝;大方無(wú)隅;大器免成;大音希聲;大象無(wú)形。" + "道隱無(wú)名。" + "夫唯道,善始且善成。"; model.addAttribute("textValue",textValue); return "demo/demo"; } }
伍、頁(yè)面效果
陸、數(shù)據(jù)渲染
VUE有個(gè)SSR(服務(wù)端渲染)的問(wèn)題比較頭疼,雖然也有解決方案(見(jiàn)我之前寫(xiě)的文章《 前后端分離Nuxt.js解決SEO問(wèn)題 》),但總覺(jué)得還是讓適合的技術(shù)做時(shí)候的業(yè)務(wù)比較好,而thymeleaf還是相當(dāng)于在服務(wù)端渲染,查看頁(yè)面源碼如下:
感謝各位的閱讀!關(guān)于“spring boot如何使用thymeleaf為模板”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文題目:springboot如何使用thymeleaf為模板
網(wǎng)頁(yè)網(wǎng)址:http://jinyejixie.com/article10/johsgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、微信公眾號(hào)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)