溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring boot項(xiàng)目如何使用thymeleaf模板

發(fā)布時(shí)間:2020-07-30 13:35:20 來(lái)源:億速云 閱讀:164 作者:小豬 欄目:開(kāi)發(fā)技術(shù)

小編這次要給大家分享的是Spring boot項(xiàng)目如何使用thymeleaf模板,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

在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),也可以通過(guò)model形式去傳遞出去,這篇博客主要是使用thymeleaf模板,將后臺(tái)數(shù)據(jù)傳遞給前臺(tái)。

2、首先要在spring boot 項(xiàng)目中添加如下依賴(lài):

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3、這里后臺(tái)有關(guān)如何查詢(xún)數(shù)據(jù),得到數(shù)據(jù)的具體過(guò)程就不在多說(shuō)了,只是寫(xiě)將數(shù)據(jù)庫(kù)中查詢(xún)到的數(shù)據(jù)取出來(lái),放到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)界面的寫(xiě)法,

<span th:text="${person.name}"></span> <span th:text="${person.age}"></span>

通過(guò)這樣的方法就可以取到放入model中的person的name和age了。

看完這篇關(guān)于Spring boot項(xiàng)目如何使用thymeleaf模板的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI