javascript
013_SpringBoot视图层技术thymeleaf-迭代遍历
1. 使用maven構(gòu)建SpringBoot的名叫spring-boot-view-thymeleaf-each項(xiàng)目
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標(biāo)簽不規(guī)范報(bào)錯(cuò) --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動(dòng)器 --><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狀態(tài)變量屬性
3.1. index: 當(dāng)前迭代器的索引, 從0開(kāi)始。
3.2. count: 當(dāng)前迭代對(duì)象的計(jì)數(shù), 從1開(kāi)始。
3.3. size: 被迭代對(duì)象的長(zhǎng)度。
3.4. even/odd: 布爾值, 當(dāng)前循環(huán)是否是偶數(shù)/奇數(shù), 從1開(kāi)始。
3.5. first: 布爾值, 當(dāng)前循環(huán)的是否是第一條, 如果是返回true, 否則返回false。
3.6. last: 布爾值, 當(dāng)前循環(huán)的是否是最后一條, 如果是則返回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>計(jì)數(shù)</th><th>長(zhǎng)度</th><th>偶數(shù)</th><th>奇數(shù)</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對(duì)象</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啟動(dòng)類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }9. 啟動(dòng)項(xiàng)目, 并使用瀏覽器訪問(wèn)迭代List
10. 啟動(dòng)項(xiàng)目, 并使用瀏覽器訪問(wèn)迭代Map?
總結(jié)
以上是生活随笔為你收集整理的013_SpringBoot视图层技术thymeleaf-迭代遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 012_SpringBoot视图层技术t
- 下一篇: 014_SpringBoot视图层技术t