2 回答
TA贡献1807条经验 获得超9个赞
这对我有用:
在控制器中实例化 item 并设置为模型:
@GetMapping("/test")
public String get(Model model) {
List<CustomItem> items = itemService.findAll();
model.addAttribute("items", items);
model.addAttribute("item", new CustomItem());
return "test";
}
HTML:
<table>
<tr th:each="i : ${items}">
<form th:action="@{/test}" method="post" th:object="${item}">
<td th:text="${i.id}" />
<td th:text="${i.name}" />
<td><input type="hidden" th:value="${i.id}" name="id" />
<input type="hidden" th:value="${i.someField}" name="someField" />
<button type="submit" name="action" value="remove">OK</button></td>
</form>
</tr>
</table>
并在控制器中创建一个方法来处理该项目:
@PostMapping("/test")
public String test(@ModelAttribute CustomItem item,HttpServletRequest request) {
doStuff(item);
}
TA贡献1836条经验 获得超3个赞
我通过简单地使用th:value和name属性而不是th:field:
<table>
<tr th:each="item : ${items}">
<td>
<form th:action="@{/test}" method="post">
<input type="text" th:value="${item.someField}" name="someField">
<button type="submit">Submit</button>
</form>
</td>
</tr>
</table>
添加回答
举报