當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
013_SpringBoot视图层技术thymeleaf-迭代遍历
生活随笔
收集整理的這篇文章主要介紹了
013_SpringBoot视图层技术thymeleaf-迭代遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 使用maven構建SpringBoot的名叫spring-boot-view-thymeleaf-each項目
2. pom.xml?
<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><groupId>com.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-each</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標簽不規范報錯 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies> </project>3. Thymeleaf迭代List狀態變量屬性
3.1. index: 當前迭代器的索引, 從0開始。
3.2. count: 當前迭代對象的計數, 從1開始。
3.3. size: 被迭代對象的長度。
3.4. even/odd: 布爾值, 當前循環是否是偶數/奇數, 從1開始。
3.5. first: 布爾值, 當前循環的是否是第一條, 如果是返回true, 否則返回false。
3.6. last: 布爾值, 當前循環的是否是最后一條, 如果是則返回true, 否則返回false。
4. 在src/main/resources/templates下新建eachList.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf迭代List</title></head><body><table border="1"><tr><th>id</th><th>姓名</th><th>年齡</th><th>索引</th><th>計數</th><th>長度</th><th>偶數</th><th>奇數</th><th>是否是第一條</th><th>是否是最后一條</th></tr><tr th:each="user,status : ${userList}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.age}"></td><td th:text="${status.index}"></td><td th:text="${status.count}"></td><td th:text="${status.size}"></td><td th:text="${status.even}"></td><td th:text="${status.odd}"></td><td th:text="${status.first}"></td><td th:text="${status.last}"></td></tr></table></body> </html>5. 在src/main/resources/templates下新建eachMap.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf迭代Map</title></head><body><table border="1"><tr><th>user對象</th></tr><tr th:each="user : ${userMap}"><td th:text="${user}"></td></tr></table><table border="1"><tr><th>id</th><th>姓名</th><th>年齡</th></tr><tr th:each="user : ${userMap}"><td th:each="entry:${user}" th:text="${entry.value.id}" ></td><td th:each="entry:${user}" th:text="${entry.value.name}"></td><td th:each="entry:${user}" th:text="${entry.value.age}"></td></tr></table></body> </html>6. 新建User.java
package com.bjbs.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}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;}}7. 新建UserController.java
package com.bjbs.controller;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.bjbs.pojo.User;@Controller public class UserController {@RequestMapping("/eachList")public String eachList(Model model) {List<User> list = new ArrayList<User>();list.add(new User(1, "張三", 20));list.add(new User(2, "李四", 22));list.add(new User(3, "王五", 24));list.add(new User(4, "趙六", 18));list.add(new User(5, "李師師", 16));model.addAttribute("userList", list);return "eachList";}@RequestMapping("/eachMap")public String eachMap(Model model) {Map<String, User> map = new HashMap<String, User>();map.put("u1", new User(1, "張三", 20));map.put("u2", new User(2, "李四", 22));map.put("u3", new User(3, "王五", 24));model.addAttribute("userMap", map);return "eachMap";} }8. 新建App.java
package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }9. 啟動項目, 并使用瀏覽器訪問迭代List
10. 啟動項目, 并使用瀏覽器訪問迭代Map?
總結
以上是生活随笔為你收集整理的013_SpringBoot视图层技术thymeleaf-迭代遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 012_SpringBoot视图层技术t
- 下一篇: 014_SpringBoot视图层技术t