日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

第七篇:Spring Boot整合Thymeleaf_入门试炼03

發(fā)布時(shí)間:2024/9/27 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第七篇:Spring Boot整合Thymeleaf_入门试炼03 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

基本語法實(shí)戰(zhàn)案例01

  • 在ThymeleafController中添加此方法
@RequestMapping("/show5")public String showInfo5(Model model) {model.addAttribute("msg", "Thymeleaf 第一個(gè)案例");model.addAttribute("key", new Date());return "index5";}
  • 在src/main/resources/templates目錄下面,新建index5.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Thymeleaf入門</title> </head> <body><span th:text="Hello"></span> <hr/> <span th:text="${msg}"></span> <hr/> <input type="text" name="username" th:value="${msg}"/> <hr/> <span th:text="${#strings.isEmpty(msg)}"/> <hr/> <span th:text="${#strings.contains(msg,'9')}"/> <span th:text="${#strings.contains(msg,'T')}"/> <hr/> <span th:text="${#strings.startsWith(msg,'a')}"/> <span th:text="${#strings.startsWith(msg,'T')}"/> <hr/> <span th:text="${#strings.endsWith(msg,'a')}"/> <hr/> <span th:text="${#strings.indexOf(msg,'h')}"/> <hr/> <span th:text="${#strings.substring(msg,13)}"/> <span th:text="${#strings.substring(msg,13,14)}"/> <hr/> <span th:text="${#strings.toUpperCase(msg)}"/> <span th:text="${#strings.toLowerCase(msg)}"/> <hr/> <span th:text="${#dates.format(key)}"/> <hr/> <span th:text="${#dates.format(key,'yyy/MM/dd')}"/> <hr/> <span th:text="${#dates.year(key)}"/> <hr/> <span th:text="${#dates.month(key)}"/> <hr/> <span th:text="${#dates.day(key)}"/> </body> </html>
  • 啟動(dòng)項(xiàng)目,訪問瀏覽器:http://localhost:8080/show5

基本語法實(shí)戰(zhàn)案例02

  • 在ThymeleafController中添加此方法
@RequestMapping("/show2")public String showInfo2(Model model) {model.addAttribute("sex", "男");model.addAttribute("id", "2");return "index2";}
  • 在src/main/resources/templates目錄下面,新建index2.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Thymeleaf入門</title> </head> <body><span th:if="${sex}=='男'">性別:男</span><span th:if="${sex}=='女'">性別:女</span><div th:switch="${id}"><span th:case="1">id為1</span><span th:case="2">id為2</span><span th:case="3">id為3</span></div> </body> </html>
  • 啟動(dòng)項(xiàng)目,訪問瀏覽器:http://localhost:8080/show2

基本語法實(shí)戰(zhàn)案例03

  • 在ThymeleafController中添加此方法
@RequestMapping("/show3")public String showInfo3(Model model) {List<User> list = new ArrayList<>();list.add(new User(1, "zhangsan", 20));list.add(new User(2, "lisi", 21));list.add(new User(3, "wangwu", 22));model.addAttribute("list", list);return "index3";}
  • 在src/main/resources/templates目錄下面,新建index3.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Thymeleaf入門</title> </head> <body> <table border="1"><tr><th>ID</th><th>Name</th><th>Age</th><th>Index</th><th>Count</th><th>Size</th><th>Even</th><th>Odd</th><th>First</th><th>Last</th><th>Current</th></tr><tr th:each="u,var:${list}"><td th:text="${u.userid}"></td><td th:text="${u.username}"></td><td th:text="${u.userage}"></td><td th:text="${var.index}"></td><td th:text="${var.count}"></td><td th:text="${var.size}"></td><td th:text="${var.even}"></td><td th:text="${var.odd}"></td><td th:text="${var.first}"></td><td th:text="${var.last}"></td><td th:text="${var.current}"></td></tr> </table> </table> </body> </html>
  • 啟動(dòng)項(xiàng)目,訪問瀏覽器:http://localhost:8080/show3

基本語法實(shí)戰(zhàn)案例04

  • 在ThymeleafController中添加此方法
@RequestMapping("/show4")public String showInfo4(Model model) {Map<String, Object> map = new HashMap<>();map.put("u1", new User(1, "zhangsan", 20));map.put("u2", new User(2, "lisi", 21));map.put("u3", new User(3, "wangwu", 22));model.addAttribute("map", map);return "index4";}
  • 在src/main/resources/templates目錄下面,新建index4.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Thymeleaf入門</title> </head> <body> <table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:text="${maps}"></td></tr> </table> <table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.key}"></td><td th:each="entry:${maps}" th:text="${entry.value}"></td></tr> </table> <table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.key}"></td><td th:each="entry:${maps}" th:text="${entry.value.username}"></td></tr> </table> <table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.value.userid}"></td><td th:each="entry:${maps}" th:text="${entry.value.username}"></td><td th:each="entry:${maps}" th:text="${entry.value.userage}"></td></tr> </table> </body> </html>
  • 啟動(dòng)項(xiàng)目,訪問瀏覽器:http://localhost:8080/show4

本文源碼下載:

github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-7

總結(jié)

以上是生活随笔為你收集整理的第七篇:Spring Boot整合Thymeleaf_入门试炼03的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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