SpringBoot中web模版數(shù)據(jù)渲染展示的案例詳解
在第一節(jié)我們演示通過(guò)接口返回?cái)?shù)據(jù),數(shù)據(jù)沒(méi)有渲染展示在頁(yè)面上 。在這里我們演示一下從后臺(tái)返回?cái)?shù)據(jù)渲 染到前端頁(yè)面的項(xiàng)目案例.
模板引擎
SpringBoot是通過(guò)模版引擎進(jìn)行頁(yè)面結(jié)果渲染的,官方提供預(yù)設(shè)配置的模版引擎主要有
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
我們?cè)谶@里演示使用Thymeleaf
和FreeMarker
模板引擎。
Thymeleaf
Thymeleaf是適用于 Web 和獨(dú)立環(huán)境的現(xiàn)代服務(wù)器端 Java 模板引擎。
Thymeleaf 的主要目標(biāo)是為你的開發(fā)工作流程帶來(lái)優(yōu)雅的自然模板——HTML可以在瀏覽器中正確顯示,也可以作為靜態(tài)原型工作,從而加強(qiáng)開發(fā)團(tuán)隊(duì)的協(xié)作。
憑借 Spring Framework 的模塊、與你最喜歡的工具的大量集成以及插入你自己的功能的能力,Thymeleaf 是現(xiàn)代 HTML5 JVM Web 開發(fā)的理想選擇——盡管它還有更多功能。
新建一個(gè)模塊
選擇我們需要的組建
Developer Tools
中的Spring Boot DevTools
Web
中的Spring Web
Template Engines
中的Thymeleaf
也可以直接在pom.xml中引入thymeleaf
依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
創(chuàng)建模板頁(yè)面
src/main/resources/templates/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>thymeleaf - 入門小站</title> </head> <body> <p th:text="'名字:'+${name}"></p> <p th:text="'網(wǎng)址:'+${url}"></p> </body> </html>
創(chuàng)建controller
com.rumenz.lession4.controller.ThymeleafRumenController
package com.rumenz.lession4.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @className: ThymeleafRumenController * @description: TODO 類描述 * @author: 入門小站 rumenz.com * @date: 2021/11/1 **/ @Controller @RequestMapping("/") public class ThymeleafRumenController { @RequestMapping(value = "/index",method= RequestMethod.GET) public String index(ModelMap m){ //數(shù)據(jù)也可以從數(shù)據(jù)庫(kù)查詢出來(lái)返回 m.addAttribute("name", "入門小站"); m.addAttribute("url", "https://rumenz.com"); //返回是一個(gè)頁(yè)碼:src/main/resources/templates/thymeleaf.html return "thymeleaf"; } }
啟動(dòng)項(xiàng)目
瀏覽器驗(yàn)證
瀏覽器訪問(wèn)http://127.0.0.1:8080/index
FreeMarker
FreeMarker是一款模板引擎,即一種基于模板和要改變的數(shù)據(jù),并用來(lái)生成輸出文本(HTML網(wǎng)頁(yè),電子郵件,配置文件,源代碼等)的通用工具。
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
創(chuàng)建模板
src/main/resources/templates/freemarker.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>freemarker - 入門小站</title> </head> <body> FreeMarker案例 <p>名字:${name}</p> <p>網(wǎng)址:${url}</p> </body> </html>
配置文件
src/main/resources/application.properties
,指定模板文件的后綴。
spring.freemarker.suffix=.html
編寫controller
com.rumenz.lession4.controller.FreeMarkerRumenController
package com.rumenz.lession4.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * @className: FreeMarkerRumenController * @description: TODO 類描述 * @author: 入門小站 rumenz.com * @date: 2021/11/1 **/ @Controller @RequestMapping("/") public class FreeMarkerRumenController { @RequestMapping("/index2") public String index2(ModelMap m){ //數(shù)據(jù)也可以從數(shù)據(jù)庫(kù)查詢出來(lái)返回 m.addAttribute("name", "入門小站"); m.addAttribute("url", "https://rumenz.com"); //返回是一個(gè)頁(yè)碼:src/main/resources/templates/freemarker.html return "freemarker"; } }
瀏覽器驗(yàn)證
瀏覽器訪問(wèn)http://127.0.0.1:8080/index2
本小結(jié)源碼地址:
GitHub:https://github.com/mifunc/springboot/tree/main/lession4
Gitee:https://gitee.com/rumenz/springboot/tree/master/lession4
https://rumenz.com/rumenbiji/springboot-tpl.html
到此這篇關(guān)于SpringBoot中web模版數(shù)據(jù)渲染展示 的文章就介紹到這了,更多相關(guān)SpringBoot模版渲染內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于JavaMail實(shí)現(xiàn)郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了基于JavaMail實(shí)現(xiàn)郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03maven?scope?provided和runtime的例子說(shuō)明
這篇文章主要介紹了maven?scope?provided和runtime的例子說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12詳解JSON與?Java對(duì)象之間的轉(zhuǎn)化
在現(xiàn)在的日常開發(fā)中,不管前端還是后端,JSON?格式的數(shù)據(jù)是用得比較多的,甚至可以說(shuō)無(wú)處不在。所以本文主要來(lái)講講JSON?格式的數(shù)據(jù)與?Java?對(duì)象之間的轉(zhuǎn)化吧2023-03-03SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot單機(jī)限流的實(shí)現(xiàn)
在系統(tǒng)運(yùn)維中, 有時(shí)候?yàn)榱吮苊庥脩舻膼阂馑⒔涌? 會(huì)加入一定規(guī)則的限流,本文主要介紹了SpringBoot單機(jī)限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08代碼詳解Java猴子選王問(wèn)題(約瑟夫環(huán))
本篇文章通過(guò)實(shí)例給大家分析了java約瑟夫環(huán)這個(gè)經(jīng)典內(nèi)容,有興趣的跟著小編一起學(xué)習(xí)下吧。2018-02-02