I just started working on Thymeleaf and wanted to connect a Spring Boot application with HTML using Thymeleaf. I had my "normal" controllers without Thymeleaf like this:
@GetMapping(path = "/{id}")
public BookEntity getBookById(@PathVariable Long id) {
return bookService.getById(id);
}
@GetMapping
public List<BookEntity> getAllBooks() {
return bookService.findAll();
}
Now that Im using Thymeleaf I created some new Controllers like this:
@GetMapping(path = "/book/{id}")
public String getBookById(@PathVariable("id") Long id, Model model) {
BookEntity book= bookService.getById(id);
model.addAttribute("book", book);
return "book/{id}";
}
@GetMapping("/book/all")
public String AllBooks(Model model) {
List<BookEntity> books = bookService.findAll();
model.addAttribute("books", books);
return "books";
}
I also created a book.html file in resources/templates like this:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<title>Books</title>
</head>
<body>
<h4>Select action to do something</h4>
<form th:action="@{/book/{id}(id=book.getId())" method="get">
<label for="id">id:</label><br>
<input type="number" id="id" name="id" placeholder="id" required>
<input type="submit" value="Submit">
</form>
</body>
</html>
But this isnt working I wanted to submit the book Id (in the input) and in return I get the Book with the right id. I tried to use ModelAndView instead of Model and return a ModelAndView but it didnt work. Can anyone tell what am I doing wrong. Thanks in advance