Piszę sobie prostego cruda (spring boot, spring data, thymyleaf) i jestem na etapie że rekordy do bazy danych się dodają ale mam problem z wyciągnięciem ich do tabelki w html'u.
BookController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class BookController {
private BookRepository bookRepository;
@Autowired
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@PostMapping("/add")
public String saveBook(@ModelAttribute Book book) {
bookRepository.save(book);
return "redirect:/";
}
@GetMapping("/show")
public String showall(Model model) {
List<Book> allBooks = bookRepository.findAll();
model.addAttribute("allBooks", allBooks);
return "showall";
}
}
ShowAllController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ShowAllController {
@GetMapping("/showall")
public String showall() {
return "showall";
}
}
showall.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>All results</title>
</head>
<body>
<h1>Books:</h1>
<table border="1">
<tr>
<th>Isbn</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Release year</th>
</tr>
<tr th:each="book: ${allBooks}">
<td th:number="${book.isbn}"/>
<td th:text="${book.title}"/>
<td th:text="${book.author}"/>
<td th:text="${book.publisher}"/>
<td th:number="${book.releaseYear}"/>
</tr>
</table>
<a th:href="@{/add}">Add book</a>
</body>
</html>
Gdzieś tu jest problem z listą?
Update:
Problem rozwiązany:
show.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>All results</title>
</head>
<body>
<div th:if="${!#lists.isEmpty(allBooks)}">
<h1>All registered books:</h1>
<table border="1">
<tr>
<th>Id</th>
<th>Isbn</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Release year</th>
</tr>
<tr th:each="book : ${allBooks}" th:object="${Book}">
<td th:text="${book.id}">id</td>
<td th:text="${book.isbn}">isbn</td>
<td th:text="${book.title}">Tytuł</td>
<td th:text="${book.author}">Autor</td>
<td th:text="${book.publisher}">Wydawca</td>
<td th:text="${book.releaseYear}">Rok wydania</td>
</tr>
</table>
</div>
<div th:if="${#lists.isEmpty(allBooks)}">
<h1>Brak książek w bazie.(</h1>
</div>
<a th:href="@{/add}">Add book</a><br>
<a th:href="@{/show}">Show all books</a><br>
<a th:href="@{/update}">Update books</a><br>
<a th:href="@{/delete}">Delete book/books</a><br>
</body>
</html>
BookController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class BookController {
private BookRepository bookRepository;
@Autowired
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/add")
public String saveBook(Book book) {
bookRepository.save(book);
return "add";
}
@GetMapping("/show")
public String showall(Model model) {
List<Book> allBooks = bookRepository.findAll();
model.addAttribute("allBooks", allBooks);
return "show";
}
@PostMapping("/update")
public String update(Book book, Long id) {
Book bookToUpdate = bookRepository.getOne(id);
bookToUpdate.setIsbn(book.getIsbn());
bookToUpdate.setTitle(book.getTitle());
bookToUpdate.setAuthor(book.getAuthor());
bookToUpdate.setPublisher(book.getPublisher());
bookToUpdate.setReleaseYear(book.getReleaseYear());
bookRepository.save(bookToUpdate);
return "redirect:/";
}
@PostMapping("/delete")
public String deleteBook() {
bookRepository.deleteAll();
return "delete";
}
}```