Reputation: 5314
I'm trying to edit data (checkboxes) in a table. But when I click on the submit button, the form in the controller is empty
I didn't find a example with thymeleaf so I adapted this example
I use a form object:
public class MyForm {
private List<MyObj> data;
// ...
}
The controller with the two methods (GET and POST):
@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
}
And my 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>
What did I forget?
Upvotes: 2
Views: 4109
Reputation: 1304
You should use the following syntax :
<tr th:each="myObj, rowStat : *{data}">
and then set the input fields using :
th:field="*{data[__${rowStat}.index}__].myField}"
That will bind your MyForm with the input data.
More examples can be found here Thymeleaf and forms
Upvotes: 3