⏹后台实体类
import lombok.Data;
import java.util.List;
@Data
public class ZTestEntity {
private List<Person> personList;
}
⏹Controller层
@Controller
@RequestMapping("test")
public class ZTestController {
@Autowired
private ZTestService service;
@GetMapping("/init")
public ModelAndView init() {
// 初始化向前台传递的实体类
ZTestEntity entity = service.init();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
modelAndView.addObject("entity", entity);
return modelAndView;
}
}
⏹前台
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:object="${entity}">
<table>
<!--
person为list中的实体类
state对象包含下面的属性
💪index,从0开始的角标
💪count,元素的个数,从1开始
💪size,总元素个数
💪current,当前遍历到的元素
💪even/odd,返回是否为奇偶,boolean值
💪first/last,返回是否为第一或最后,boolean值
-->
<tbody th:each="person,state : *{personList}">
<!--
属性值拼接的情况使用 |拼接内容|
-->
<tr th:id="|row${state.index}|">
<td>
[[${person.personId}]]
</td>
<!--
当使用多个属性的时候,需要用 , 隔开
-->
<td th:attr="data-personname=|前缀${person.personName}|,data-tdindex=${state.index}">
[[${person.personName}]]
</td>
<td>
[[${person.personAge}]]
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
⏹渲染完毕效果