【发布时间】:2019-02-03 09:13:08
【问题描述】:
在我的 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>
但我在导航到页面时收到以下错误:
Bean 名称“items[0]”的 BindingResult 和普通目标对象都不是 可用作请求属性
我也尝试了以下方法:
<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>
在这种情况下,错误如下:
Bean 名称“item”的 BindingResult 和普通目标对象都不是 可用作请求属性
我无法弄清楚我的方法有什么问题,所以我非常感谢任何建议。
编辑:
正如@StefanEmanuelsson 建议的那样,我尝试省略th:object 属性:
<table>
<tr th:each="item, stat : ${items}">
<td>
<form th:action="@{/test}" method="post">
<input type="text" th:field="${items[__${stat.index}__].someField}">
<button type="submit">Submit</button>
</form>
</td>
</tr>
</table>
这样页面加载得很好,但是在提交表单时,控制器中收到的(?)CustomItem 中的someField 的值是null。
【问题讨论】:
-
The thymeleaf docs 说:>“表单标签中 th:object 属性的值必须是变量表达式 (${...}),仅指定模型属性的名称,没有属性导航。这意味着像 ${seedStarter} 这样的表达式是有效的,但 ${seedStarter.data} 不是。”我猜您的代码有资格作为属性导航,因为您将迭代变量用作 th:object?我会尝试使用 th:field="${item.someField}" 而不是 th:object.
-
@StefanEmanuelsson 感谢您的回复,请检查我问题末尾的编辑。
标签: java spring spring-mvc thymeleaf