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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot2 学习5集成Thymeleaf

發布時間:2023/12/18 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2 学习5集成Thymeleaf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

整個項目文件結構

API 參考地址:https://www.e-learn.cn/thymeleaf/standard-dialects

POM

關鍵:

org.springframework.boot
spring-boot-starter-thymeleaf

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>zz</groupId><artifactId>SpringBoot2</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>SpringBootThymeleaf</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--目的:《可選》引入springboot 熱啟動,每次修改以后,會自動把改動加載,不需要重啟服務了--><dependency> <groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.15</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><repositories><repository><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository></repositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>

Controller

package com.zz.controller;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;import com.zz.entity.Account;@Controller public class IndexController {@RequestMapping("/account")public String index(Model m) {List<Account> list = new ArrayList<Account>();list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超級管理員", "17777777777"));list.add(new Account("Mike", "麥克", "e10adc3949ba59abbe56e", "管理員", "13444444444"));list.add(new Account("Jane","簡","e10adc3949ba59abbe56e","運維人員","18666666666"));list.add(new Account("Maria", "瑪利亞", "e10adc3949ba59abbe56e", "清算人員", "19999999999"));m.addAttribute("accountList",list);return "account";} }

template

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head><title>account</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" th:href="@{/css/style.css}" type="text/css"> </head> <body><table><tr><th>no</th><th>account</th><th>name</th><th>password</th><th>accountType</th><th>tel</th></tr><tr th:each="list,stat : ${accountList}"><td th:text="${stat.count}"></td><td th:text="${list.account}"></td><td th:text="${list.name}"></td><td th:text="${list.password}"></td><td th:text="${list.accountType}"></td><td th:text="${list.tel}"></td></tr></table> </body> </html>

測試效果

語法

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>demo1</title> </head> <body><h1>${...} : 變量表達式。 演示</h1><h3>1 取值,直接顯示</h3> <span >你好</span><span th:text="${helloname}"></span><h3>2 對象,顯示</h3><span th:text="${a.name}"></span><h3>3 List,顯示</h3> 遍歷(迭代)的語法th:each="自定義的元素變量名稱 : ${集合變量名稱}": 狀態變量的使用語法:th:each="自定義的元素變量名稱, 自定義的狀態變量名稱 : ${集合變量名稱}": 不管什么時候,Thymeleaf 始終會為每個th:each創建一個狀態變量, 默認的狀態變量名稱就是自定義的元素變量名稱后面加Stat字符串組成<table border="1"> <tr> <th>名字</th> <th>電話</th> </tr><tr th:each="c:${cs}"><td th:text="${c.name}"></td><td th:text="${c.tel}"></td> </tr></table></body> </html>

controller

package com.zz.controller;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;import com.zz.entity.Account;@Controller @RequestMapping("/thymeleaf") public class DemoController {@RequestMapping("/demo1")public String to_demo1(Model m) {m.addAttribute("helloname", "jerry");Account account=new Account();account.setName("小明");m.addAttribute("a", account);Account account1=new Account();account1.setName("小明");account1.setTel("23424");Account account2=new Account();account2.setName("小紅");account2.setTel("322");Account account3=new Account();account3.setName("小王");account3.setTel("111");List<Account> clist=new ArrayList();clist.add(account1);clist.add(account2);clist.add(account3);m.addAttribute("cs", clist);return "demo1";} }

總結

以上是生活随笔為你收集整理的SpringBoot2 学习5集成Thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。