Reputation: 11
I am working with spring boot and thymeleaf and I keep getting the "Neither BindingResult nor plain target object for bean name bookDto
available as request attribute" when I return the index page after successful login
The weird thing is that the index page works fine when I search for it as localhost:8080/index
.
I have also tried returning another page from login method and its only the index page giving me the error.
Below are my controllers methods and the index.html form
@RequestMapping(value = "/login")
@PostMapping
public ModelAndView login(@ModelAttribute("loginForm") LoginForm loginForm, ModelAndView modelAndView, BindingResult bindignResult) {
AppUser userLoggedIn = userService.findByUserNameAndEncryptedPassword(loginForm.getUsername(), loginForm.getPassword());
if (userLoggedIn == null){
modelAndView.addObject("errorLogin", "Incorrect Credentials!");
modelAndView.setViewName("signin");
}else {
modelAndView.setViewName("index");
}
return modelAndView;
}
filters books by their attributes
@RequestMapping(value = "/search")
@PostMapping
public ModelAndView searchBooks(@ModelAttribute ("bookDto") BookDto bookDto, Model model) {
model.addAttribute("books", bookService.listAll(searchFilterBuilder.buildFilter(bookDto)));
model.addAttribute("bookDto", bookDto);
return new ModelAndView("index");
}
and finally my in index.html
<form th:object="${bookDto}" th:action="@{/search}" method="post">
Upvotes: 0
Views: 49
Reputation: 36103
The bookDto is sent as the request body so you have to get it like this
public ModelAndView searchBooks(BookDto bookDto, Model model) {
Simply remove @ModelAttribute
But you have to make sure that bookDto get's initialized! So when you are navigating to the index page you have to create a new BookDto:
modelAndView.setViewName("index");
model.addAttribute("bookDto", new BookDto());
Upvotes: 0