春季靴和百里角 - 显示所有元素
您能帮我显示“ book-data” .html文件中的所有元素?
我的控制器类是
@Autowired BooksService booksService
@GetMapping("/books")
private List<Books> getAllBooks() throws IOException, InterruptedException {
return booksService.getAllBooks();
}
,我的服务类是
@Autowired BooksRepository booksRepository;
public List<Books> getAllBooks() throws IOException, InterruptedException {
return (List<Books>) booksRepository.findAll();
}
我的书籍存储库接口
public interface BooksRepository extends CrudRepository<Books, Integer>
{
}
,我的“ book-data” .html文件
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Book ID</th>
<th scope="col">Book Name</th>
<th scope="col">Book Author</th>
<th scope="col">Book Price</th>
</tr>
</thead>
<tbody>
<tr>
<td th:text="${book.bookid}"></td>
<td th:text="${book.bookname}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
</tr>
</tbody>
在HTML页面中,它没有显示任何书籍数据。对于服务课,我看到了下面的另一个解决方案,但没有解决问题。
@RequestMapping("/books")
private String listBooks(Model model){
model.addAtrribute("books", booksService.getAllBooks());
return "books";
}
在html中:
<tbody>
<tr th:each="book : ${books}">
<td th:text="${book.bookid}"></td>
<td th:text="${book.bookname}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
</tr>
</tbody>
Can you help me about showing all the elements in "book-data".html file?
My controller class is
@Autowired BooksService booksService
@GetMapping("/books")
private List<Books> getAllBooks() throws IOException, InterruptedException {
return booksService.getAllBooks();
}
and my service class is
@Autowired BooksRepository booksRepository;
public List<Books> getAllBooks() throws IOException, InterruptedException {
return (List<Books>) booksRepository.findAll();
}
my book repository interface is
public interface BooksRepository extends CrudRepository<Books, Integer>
{
}
and my "book-data".html file is
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Book ID</th>
<th scope="col">Book Name</th>
<th scope="col">Book Author</th>
<th scope="col">Book Price</th>
</tr>
</thead>
<tbody>
<tr>
<td th:text="${book.bookid}"></td>
<td th:text="${book.bookname}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
</tr>
</tbody>
In HTML page, it doesn't show any book data. For service class, I saw another solution like below, but it didn't solve the problem.
@RequestMapping("/books")
private String listBooks(Model model){
model.addAtrribute("books", booksService.getAllBooks());
return "books";
}
in html:
<tbody>
<tr th:each="book : ${books}">
<td th:text="${book.bookid}"></td>
<td th:text="${book.bookname}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
</tr>
</tbody>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
I think something like this is maybe a solution :
in your controller with adding
@Controller
in class statement :your service with adding
@Service
in class statement :your repository with adding <代码> @repository 在类语句中:
也许您可以拥有
@query
和您的html:
I think something like this is maybe a solution :
in your controller with adding
@Controller
in class statement :your service with adding
@Service
in class statement :your repository with adding
@Repository
in class statement :maybe you could had
@Query
And your html :
首先,我创建了另一个显示“所有书籍”的HTML文件。此文件命名为
allbooks.html
。然后,下面的服务方法解决了问题,以获取您的信息。First, I created another html file that shows "all books". This file is named
allBooks.html
. Then, the service method below solved the problem, for your information.