# Spring Boot & thymeleaf 遍历对象数组 -生成一批标签体

在controller中取出emps 对象数组

//1.查询所有的员工,返回列表页面
    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();

        //放在model请求域中
        model.addAttribute("emps",employees);

        //thymeleaf 默认就会拼串 //public static final String DEFAULT_PREFIX = "classpath:/templates/xxx.html";
        //public static final String DEFAULT_SUFFIX = ".html";
        return "emp/list";
    }

在list.html中进行遍历

<tbody>
                                <tr th:each="emp:${emps}">
                                    <td th:text="${emp.id}"></td>
                                    <td th:text="${emp.lastName}"></td>
                                    <td th:text="${emp.email}"></td>
                                    <td th:text="${emp.gender}==1?'男':'女'"></td>
                                    <td th:text="${emp.department.departmentName}"></td>
                                    <td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd')}"></td>
                                    <td>
                                        <button class="btn btn-sm btn-primary">编辑</button>
                                        <button class="btn btn-sm btn-danger">删除</button>
                                    </td>
                                </tr>
                            </tbody>

结果展示

猜你喜欢

转载自www.cnblogs.com/zhazhaacmer/p/10432527.html