Em chào mọi người, em đang học spring boot, đến phần handle form thì em đang không bắt được dữ liệu gửi lên từ form bằng phương thức POST, mọi người giúp em với ạ.
Đây là code của form:
<form th:action="@{/addTodo}" method="POST" th:object="${ToDo}" class="form" role="form">
<div class="form-group">
<label>Name</label>
<input
type="text"
class="form-control"
placeholder="Input field"
value="Test Name"
th:field="*{_todoName}"
/>
</div>
<div class="form-group">
<label>Description</label>
<input
type="text"
class="form-control"
placeholder="Input field"
value="Test des"
th:field="*{_todoDescription}"
/>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Code controller:
@PostMapping("/addTodo")
public String AddTodo(@ModelAttribute Todo ToDo,Model model,@RequestParam Map<String,String> allRequestParams) {
System.out.println(allRequestParams); //Nhận được dữ liệu
System.out.println(model); //Nhận được đối tượng Todo là null
System.out.println(ToDo); //Nhận được đối tượng Todo là null
return "redirect:/to-do-list";
}
Model Todo
@Data
@AllArgsConstructor
public class Todo {
private String _todoName;
private String _todoDescription;
}
Khi tiến hành gửi từ form lên controller, đây là kết quả em bắt bằng debug:
Em không thể lấy giá trị bằng @ModelAttribute là vì sao ạ?
Rất mong được mọi người giúp đỡ, em xin chân thành cảm ơn ạ.