Spring boot項(xiàng)目使用thymeleaf模板過程詳解
在spring boot 項(xiàng)目中使用thymeleaf模板,將后臺(tái)數(shù)據(jù)傳遞給前臺(tái)界面。
1、將后臺(tái)數(shù)據(jù)傳遞給前臺(tái)有很多種方式,可以將后臺(tái)要傳遞的數(shù)據(jù)轉(zhuǎn)換成json格式,去傳遞給前臺(tái),也可以通過model形式去傳遞出去,這篇博客主要是使用thymeleaf模板,將后臺(tái)數(shù)據(jù)傳遞給前臺(tái)。
2、首先要在spring boot 項(xiàng)目中添加如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3、這里后臺(tái)有關(guān)如何查詢數(shù)據(jù),得到數(shù)據(jù)的具體過程就不在多說了,只是寫將數(shù)據(jù)庫中查詢到的數(shù)據(jù)取出來,放到model里面。這里就一個(gè)例子吧。
@RequestMapping("/")
public String index(Model model){
Person single=new Person("aa",11);
List<Person> people =new ArrayList<Person>();
Person p1=new Person("xx",22);
Person p2=new Person("dd",33);
Person p3=new Person("zz",44);
people.add(p1);
people.add(p2);
people.add(p3);
model.addAttribute("singlePerson",single);
model.addAttribute("people",people);
return "index";
}
4.前臺(tái)界面的寫法,
<span th:text="${person.name}"></span> <span th:text="${person.age}"></span>
通過這樣的方法就可以取到放入model中的person的name和age了。
(注:前臺(tái)界面要添加上這個(gè)代碼:<html xmlns:th="http://www.thymeleleaf.org">)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot thymeleaf模板引擎的使用詳解
- SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
- springBoot加入thymeleaf模板的方式
- spring boot 項(xiàng)目中使用thymeleaf模板的案例分析
- SpringBoot使用thymeleaf模板過程解析
- Spring Boot集成Thymeleaf模板引擎的完整步驟
- SpringBoot中的Thymeleaf模板
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
- 詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
- Springboot Thymeleaf模板文件調(diào)用Java類靜態(tài)方法
- Java基礎(chǔ)總結(jié)之Thymeleaf詳解
相關(guān)文章
Java 遞歸遍歷實(shí)現(xiàn)linux tree命令方式
這篇文章主要介紹了Java 遞歸遍歷實(shí)現(xiàn)linux tree命令方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
詳解Java設(shè)計(jì)模式編程中的Flyweight享元模式的開發(fā)結(jié)構(gòu)
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的Flyweight享元模式的開發(fā)結(jié)構(gòu),享元模式能夠最大限度地重用現(xiàn)有的同類對(duì)象,需要的朋友可以參考下2016-04-04
Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
spring boot實(shí)戰(zhàn)教程之shiro session過期時(shí)間詳解
這篇文章主要給大家介紹了關(guān)于spring boot實(shí)戰(zhàn)教程之shiro session過期時(shí)間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-10-10
SpringBoot項(xiàng)目的五種創(chuàng)建方式
這篇文章主要介紹了SpringBoot項(xiàng)目的五種創(chuàng)建方式,文中通過圖文結(jié)合的方式講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12

