在我的 Spring Boot 应用程序中,我有以下内容RequestMapping:
@GetMapping("/test")
public String get(Model model) {
List<CustomItem> items = itemService.findAll();
model.addAttribute("items", items);
return "test";
}
我将这些项目显示在一个简单的 HTML 表格中(一行表示一个项目)。
我想向每一行添加一个按钮,该按钮仅提交与CustomItem端点相对应的内容,如下所示:
@PostMapping("/test")
public String post(CustomItem item) {
// doing something with item
return "redirect:/test";
}
我试过的是form为每一行创建一个单独的:
<table>
<tr th:each="item, stat : ${items}">
<td>
<form th:object="${items[__${stat.index}__]}" th:action="@{/test}" method="post">
<input type="text" th:field="${items[__${stat.index}__].someField}">
<button type="submit">Submit</button>
</form>
</td>
</tr>
</table>
但是我在导航到页面时收到以下错误:
BindingResult 和 bean 名称“items[0]”的普通目标对象都不能用作请求属性
我也尝试了以下方法:
<table>
<tr th:each="item, stat : ${items}">
<td>
<form th:object="${item}" th:action="@{/test}" method="post">
<input type="text" th:field="*{someField}">
<button type="submit">Submit</button>
</form>
</td>
</tr>
</table>
在这种情况下,错误如下:
BindingResult 和 bean 名称“item”的普通目标对象都不能用作请求属性
我无法弄清楚我的方法有什么问题,所以我真的很感激任何建议。
函数式编程
米脂
相关分类