Iterative processing of thymeleaf data

 

Data iteration is an important part of the template language and of all project development, so the following iteration will be implemented and the iteration type of data in actual development will often have List Map type

1.0 Establish a controller to append a List collection data to the controller

/****
* Simulate Collection Data to Templates How Templates Display
* @param model
* @return
*/
@RequestMapping(value = "/user_list",method = RequestMethod.GET)
public String userList(Model model){
    ArrayList<User> arrayList =new ArrayList<>();
    for (int i=0;i<10;i++){
        User user=new User();
        user.setMid(102L);
        user.setName("slightly");
        user.setAge(18);
        user.setBirthday(new Date());
        user.setSalary(9999969.96);
        arrayList.add(user);
    }
    model.addAttribute("user",arrayList);
    return  "message/message_list";
}
2.0 Create member_list.html page
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type"  content="text/html;charset=UTF-8" >
    <title>Template Collection Traversal</title>
</head>
<body>
  <table style="border: red" border="1">
      <tr><td>mid</td> <td>Full name</td><td>Age</td></tr>
       <tr th:each="user,userarr:${user}">

          <td th:text="${user.mid}"></td>
           <td th:text="${user.name}"></td>
           <td th:text="${user.age}"></td>
       </tr>


  </table>
</body>
</html>

3.0 can also output more powerful content when using thymeleaf output

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--suppress ALL-->
<head>
    <meta http-equiv="Content-Type"  content="text/html;charset=UTF-8" >
    <title>Template Collection Traversal</title>
</head>
<body>
  <table style="border: red" border="1">
      <tr><td>NO</td><td>mid</td> <td>Full name</td><td>Age</td><td>Odd Number</td><td>Even numbers</td></tr>
       <tr th:each="user,userarr:${user}">
           <td th:text="${userarr.index+1}"></td>
          <td th:text="${user.mid}"></td>
           <td th:text="${user.name}"></td>
           <td th:text="${user.age}"></td>
           <td th:text="${userarr.even}"></td>
           <td th:text="${userarr.odd}"></td>
       </tr>


  </table>
</body>
</html>

4.0 In addition to outputting a List collection, a Map collection is often an output form of operation in actual development, so here's how to iterate the output of a Map collection.The output of a Map collection must be done using the Map.Entry interface, which can only be obtained using the getKey() or getValue() methods.

/****
* Map Simulate Collection Data to Templates How Templates Display
* @param model
* @return
*/
@RequestMapping(value = "/user_map",method = RequestMethod.GET)
public String userMap(Model model){
    Map<String,User> userMap=new HashMap<>();
    for (int i=0;i<10;i++){
        User user=new User();
        user.setMid(102L);
        user.setName("slightly");
        user.setAge(18);
        user.setBirthday(new Date());
        user.setSalary(9999969.96);
        userMap.put(i+"",user);
    }
    model.addAttribute("user",userMap);
    return  "message/message_map";
}

5.0 When performing page iteration output, the Map.Entry object must be the data type of each iteration obtained, so the corresponding data content must be obtained by the getKey(), getValue() methods.

<!DOCTYPE html>
<!--suppress ALL-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta   http-equiv="Content-Type"   content="text/html;charset=UTF-8" >
    <title>thymeleaf Map Collection traversal</title>
</head>
<body>
<table border="1">
    <tr><td>NO</td><td>Key</td><td>mid</td> <td>Full name</td><td>Age</td><td>Odd Number</td><td>Even numbers</td></tr>
    <tr th:each="user,userarr:${user}">
        <td th:text="${userarr.index+1}"></td>
        <td th:text="${user.getKey()}"></td>
        <td th:text="${user.getValue().mid}"></td>
        <td th:text="${user.getValue().name}"></td>
        <td th:text="${user.getValue().age}"></td>
        <td th:text="${userarr.even}"></td>
        <td th:text="${userarr.odd}"></td>
    </tr>
</table>
</body>
</html>

The githua code is as follows   https://github.com/zhouwei520/SpringbootDemo/tree/master/demo9 

Keywords: Thymeleaf github

Added by rathlon on Wed, 04 Dec 2019 04:40:29 +0200