春季靴和百里角 - 显示所有元素

发布于 2025-01-24 07:11:33 字数 1847 浏览 1 评论 0原文

您能帮我显示“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2025-01-31 07:11:33

I think something like this is maybe a solution :

in your controller with adding @Controller in class statement :

@Autowired
BooksService booksService

@GetMapping("/books")
private String listBooks(Model model){
  model.addAtrribute("books", booksService.getAllBooks());
  return "views/book-data";
}

your service with adding @Service in class statement :

@Autowired
BooksRepository bookRepository

public List<Books> getAllBooks(){
  return this.booksRepository.findAll();
}

your repository with adding <代码> @repository 在类语句中:
也许您可以拥有@query

@Repository
public interface BooksRepository extends CrudRepository<Books, Long>
{
    public List<Books> findAll();
}

和您的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 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>
</table>

I think something like this is maybe a solution :

in your controller with adding @Controller in class statement :

@Autowired
BooksService booksService

@GetMapping("/books")
private String listBooks(Model model){
  model.addAtrribute("books", booksService.getAllBooks());
  return "views/book-data";
}

your service with adding @Service in class statement :

@Autowired
BooksRepository bookRepository

public List<Books> getAllBooks(){
  return this.booksRepository.findAll();
}

your repository with adding @Repository in class statement :
maybe you could had @Query

@Repository
public interface BooksRepository extends CrudRepository<Books, Long>
{
    public List<Books> findAll();
}

And your 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 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>
</table>
那支青花 2025-01-31 07:11:33

首先,我创建了另一个显示“所有书籍”的HTML文件。此文件命名为allbooks.html。然后,下面的服务方法解决了问题,以获取您的信息。

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="https://thymeleaf.org">
<style>
    table, th, td {
        border: 1px solid black;
        text-align: center;
    }
</style>
<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  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>
@GetMapping("/books")
private ModelAndView getAllBooks() throws IOException, InterruptedException {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("allBooks");
    mav.addObject("books", booksService.getAllBooks());
    return mav;
}

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.

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="https://thymeleaf.org">
<style>
    table, th, td {
        border: 1px solid black;
        text-align: center;
    }
</style>
<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  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>
@GetMapping("/books")
private ModelAndView getAllBooks() throws IOException, InterruptedException {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("allBooks");
    mav.addObject("books", booksService.getAllBooks());
    return mav;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文