My BookController:
@GetMapping("/{id}")
public String showBook(Model model, @PathVariable("id") Integer id, @ModelAttribute("review") Review review){
if(bookService.findOne(id).isPresent()) {
model.addAttribute("book", bookService.findOne(id).get());
System.out.println(SecurityContextHolder.getContext().getAuthentication().getName());
model.addAttribute("currentUser", SecurityContextHolder.getContext().getAuthentication().getName());
return "/books/show";
}
return "redirect:/books";
}
I want to send this current Book object to CardController and get it in CardController. How I can do that?
I made a form, but I don't know how to send an object
<form th:unless="${#strings.equals(currentUser, 'anonymousUser')}" th:method="POST" th:action="@{/card}">
<button id="add-button" class="fw primary" type="submit">Add book to the card.</button>
</form>
You have modeled showBook() as a GET method so you don't need or want to use a form and POST, a link will do the trick and is more appropriate. Try:
<a id="add-button" class="fw primary" th:href="@{/books/{id}(id=${book.id})}">Add book to the card.</a>
This assumes you have @RequestMapping("/books") on the controller class and the page from which you are creating the link has access to a book in the model that has an id attribute.
If you want a button instead of a link style the link accordingly.