在Thymeleaf中,可以使用对象迭代来为多个对象填写字段。以下是一个示例:
@GetMapping("/form")
public String showForm(Model model) {
List<User> userList = userService.getAllUsers();
List<Address> addressList = addressService.getAllAddresses();
model.addAttribute("userList", userList);
model.addAttribute("addressList", addressList);
return "form";
}
<form>
<table>
<thead>
<tr>
<th>User Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userList}">
<td><input type="text" th:field="*{userList[__${userStat.index}__].userName}" /></td>
<td><input type="text" th:field="*{addressList[__${userStat.index}__].address}" /></td>
</tr>
</tbody>
</table>
<button type="submit">Submit</button>
</form>
在上述代码中,我们使用了Thymeleaf的对象迭代语法th:each
来遍历userList
,并使用userStat.index
来获取当前迭代的索引。然后,我们使用th:field
来绑定表单字段到对应的对象属性上。
这样,当表单提交时,控制器就能够接收到填写的字段值,并进行相应的处理。
推荐的腾讯云相关产品:腾讯云服务器(CVM)和腾讯云数据库(TencentDB)。
领取专属 10元无门槛券
手把手带您无忧上云