In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the iterative method of Thymeleaf data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Thymeleaf data iterations use the th:each attribute, which can iterate over arrays, List, Set, Map, and so on. Arrays, List, and Set are iterated in a similar way, and Map iterates to get a java.util.Map.Entry object.
During the iteration process, you can also get variables of the iteration state, such as iterative index, data set size, and so on.
Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8
Create a new Spring Boot project named demo.
1 、 pom.xml
Join Thymeleaf dependency
Org.springframework.boot spring-boot-starter-thymeleaf
2 、 src/main/java/com/example/demo/user.java
Package com.example.demo;public class User {Integer id; String name; public User (Integer id, String name) {this.id = id; this.name = name;} 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;}}
3 、 src/main/java/com/example/demo/TestController.java
Package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;@Controllerpublic class TestController {@ RequestMapping ("/") public String test (Model model) {List users = new ArrayList (); users.add (new User (1, "Zhang San")) Users.add (new User (2, "Li Si"); users.add (new User (3, Wang Wu)); model.addAttribute ("users", users); Map userMap = new HashMap (); userMap.put ("a", new User (4, "Zhao Liu"); userMap.put ("b", new User (5, "Qian Qi"); model.addAttribute ("userMap", userMap) Return "test";}}
4 、 src/main/resources/templates/test.html
Title table {border-collapse:collapse;} td {border: 1px solid # C1DAD7 } iterative List iterative Map iterative state object the iterState above the iterative state object is an explicitly declared iterative state object, which can also be obtained by default planning "node variable name + Stat"
Browser visit: http://localhost:8080. The screenshot is as follows:
Right-click to view the source code of the web page and generate the HTML source code:
Title table {border-collapse:collapse;} td {border: 1px solid # C1DAD7 } iterative List 1 Zhang San 2 Li Si 3 Wang Wu iterative Map a murmur4muri-Zhao Liub mur5Muy-Qian Qi iterative state object index starting from 0: 0 index starting with 1: 1 Data set size: 3 current node: 0 first iteration: true last iteration: false even iteration: false odd iteration: true index starting from 0: 1 index starting from 1: 2 data set size: 3 current section Point: 1 whether the first iteration: false last iteration: false even iteration: true odd iteration: false index starting from 0: 2 index starting from 1: 3 data set size: 3 current node: 2 whether the first iteration: false Whether the last iteration: whether true is even iteration or false is odd iteration: iterState above true is an explicitly declared iteration state object You can also get 0121 Zhang San 2 Li Si 3 Wang Wu "what is the method of Thymeleaf data iteration" according to the default planning "Node variable name + Stat". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.