@RequestMapping(value="/add-todo", method = RequestMethod.GET)
public String showAddToDoPage(ModelMap model){
model.addAttribute("todo", new ToDo());
return "add-todo";
}
@RequestMapping(value = "/add-todo", method = RequestMethod.POST)
public String addToDo(ModelMap model, @Valid ToDo todo, @ModelAttribute("todo") Date targetdate, @ModelAttribute("todo") String description, BindingResult result){
if(result.hasErrors()){
return "todo";
}
todo.setUserName(getLoggedInUserName(model));
todo.setTargetDate(targetdate);
todo.setUserDescription(description);
toDoService.saveToDo(todo);
return "redirect:/list-todo";
}
<form action="#" th:action="@{/add-todo}" th:object="${todo}" method="POST">
<td> <input type="number" th:field="*{id}" /> </td>
<td> <input type="date" th:field="*{targetdate}"/> </td>
<td> <input type="text" th:field="*{description}"/> </td>
<td> <input type="text" th:field="*{username}"/> </td>
<td><button type="submit" class="btn btn-success">Add TODO</button></td>
</form>
我使用的是上面的表格和控制器。在数据库中未插入描述的情况下,插入了系统日期,而不是从表格中输入的日期。