當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot使用thymeleaf
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot使用thymeleaf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
thymeleaf音譯:塞姆理符
1.新建SpringBoot項目,選擇thymeleaf依賴,會自動導入
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>- 1
- 2
- 3
- 4
2.創建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.創建演示頁面,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.注入數據
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.運行效果
轉載自:https://blog.csdn.net/JHYPXS/article/details/78080231
總結
以上是生活随笔為你收集整理的SpringBoot使用thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot中使用Thymele
- 下一篇: SpringBoot中使用Thymele