我正在尝试编辑表中的数据(复选框)。但是当我点击提交按钮时, Controller 中的表单是空的
我没有找到 thymeleaf 的例子,所以我改编了this example
我使用表单对象:
public class MyForm {
private List<MyObj> data;
// ...
}
具有两种方法(GET 和 POST)的 Controller :
@RequestMapping(value="/edit/", method = RequestMethod.GET)
public String editGet(Model model) {
MyForm form = new MyForm(data);
model.addAttribute("myForm", form);
return "editPage";
}
@RequestMapping(value="/edit/save", method = RequestMethod.POST)
public String editPost(@ModelAttribute("myForm") MyForm myForm, Model model) {
// here myForm.getData() is empty
}
还有我的 html:
<form th:action="@{/edit/save}" th:object="${myForm}" method="POST">
<table>
<thead>....</thead>
<tbody th:each="myObj : *{data}" th:remove="tag">
<tr>
<td><span th:text="${myObj.name}"></span></td>
<td><input type="checkbox" th:checked="${myObj.isOK}"/></td>
</tr>
</tbody>
</table>
<input type="submit">Save</input>
</form>
我忘记了什么?
最佳答案
您应该使用以下语法:
<tr th:each="myObj, rowStat : *{data}">
然后使用以下命令设置输入字段:
th:field="*{data[__${rowStat}.index}__].myField}"
这会将您的 MyForm 与输入数据绑定(bind)。
可以在此处找到更多示例 Thymeleaf and forms
关于java - 使用 SpringMVC 和 Thymeleaf 提交时出现空表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18634697/