springBoot

66 阅读1分钟

遍历页面数据

控制层代码:

@GetMapping("dynamic_table")
public String dynamic_table(Model model){ // 需要将数据存储在request域中
    // 遍历表格内容,由于还没有连接数据库,所以手动new对象
    List<User> users = Arrays.asList(new User("zhangsan","123"),
                                    new User("lisi","1234"),
                                    new User("wangwu","321"),
                                    new User("zhaoliu","4321"));
    
    // 将数据存储在request域中
    model.addAttribute("users",users);
    return "table/dynamic_table";
    
}

对应的页面代码:

<table class="display table table-bordered" id = "hidden-table-info">
    <thead>
        <tr>
            <th>#</th>
            <th>用户名</th>
            <th>密码</th>
        </tr>
    </thead>
    <tbody>
        <!--stats:用于保存遍历状态-->
        <tr th:each="user,stats:${users}">
            <!--status.count计数,从1开始,对应id-->
            <td th:text="${status.count}">Trident</td> 
            <!--标签取值方式-->
            <td th:text="${user.userName}">zhangsan</td>
            <!--直接取值方式-->
            <td>[[${user.password]]</td>
        </tr>
    </tbody>
</table>

对应效果:

result.png