日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot使用thymeleaf

發(fā)布時(shí)間:2025/3/19 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot使用thymeleaf 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

thymeleaf音譯:塞姆理符

1.新建SpringBoot項(xiàng)目,選擇thymeleaf依賴,會(huì)自動(dòng)導(dǎo)入

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4

2.創(chuàng)建POJO

package com.cvsea;public class Person {private String name;private Integer age;public Person(String P_Name,Integer P_Age){this.name=P_Name;this.age=P_Age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3.創(chuàng)建演示頁面,thymeleaf模板引擎頁面放在src/main/resources/templates下。

index.html

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body><div><h3>訪問model</h3><span th:text="${singlePeson.name}"></span> </div><div th:if="${not #lists.isEmpty(people)}"><h3>訪問列表</h3><ul><li th:each="person:${people}"><span th:text="${person.name}"></span><span th:text="${person.age}"></span><button th:onclick="'getName(\''+${person.name}+'\')'">獲取名字</button></li></ul> </div> <script th:inline="javascript"> var single=[[${singlePeson}]] console.log(single.name+"/"+single.age); function getName(name) {console.log(name); } </script> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

4.注入數(shù)據(jù)

package com.cvsea;import java.util.ArrayList; import java.util.List;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@Controller @SpringBootApplication public class Learning1Application {@RequestMapping("/")public String hello(Model model){Person person=new Person("pxs",26);model.addAttribute("singlePeson",person);List<Person> people=new ArrayList<Person>();people.add(new Person("pxs",26));people.add(new Person("nxy",26));people.add(new Person("lgp",26));model.addAttribute("people",people);return "index"; }public static void main(String[] args) {SpringApplication.run(Learning1Application.class, args);} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

5.運(yùn)行效果

轉(zhuǎn)載自:https://blog.csdn.net/JHYPXS/article/details/78080231

總結(jié)

以上是生活随笔為你收集整理的SpringBoot使用thymeleaf的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。