java - Spring Boot + Thymeleaf : How to show books and their authors in one list on the page

标签 java spring-boot thymeleaf

这里是初学者。我不知道如何在页面上的一个列表中显示书籍及其作者。我的第一个项目经过简化,如下所示:

Book.java

@Entity
@Table(name = "book")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "author")
    **private String author;**

    @Column(name = "title")
    private String title;

    @Column(name = "isbn")
    private String isbn;

    public Book() {
    }

    public Book(String author, String title, String isbn) {
        this.author = author;
        this.title = title;
        this.isbn = isbn;
    }
(...)
}

IndexController.java

@Controller
public class IndexController {

    private BookService bookService;

    @Autowired
    public IndexController(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping({"/", "", "/index"})
    public String showAll(Model model) {
        model.addAttribute("books", bookService.getAllBooks());

        return "index";
    }
}

index.html( thymeleaf )

<table>
    <thead>
    <tr>
        <th> Number</th>
        <th> Author</th>
        <th> Title</th>
        <th> ISBN</th>
    </tr>
    </thead>

    <tbody>
    <tr th:if="${books.isEmpty()}">
        <td colspan="3"> No records</td>
    </tr>

    <tr th:each="book, stat : ${books}" th:object="${book}">
        <!--        added for numbering purpose, variable stat is needed for iteration -->
        <td th:text="${stat.count}">1</td>
        <td th:text="*{author}"> Author</td>
        <td th:text="*{title}"> Title</td>
        <td th:text="*{isbn}"> ISBN</td>
    </tr>
    </tbody>
</table>

没关系,数据在我的index.html 图书列表中以这种方式工作。 但问题是,一本书可能有很多作者,很多作者有很多书,所以我必须改进我的模型,使其看起来像这样:

Book.java

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;
    private String isbn;

    @ManyToMany
    @JsonIgnore // removed wierd loop with data
    @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
            inverseJoinColumns = @JoinColumn(name = "author_id"))
    **private Set<Author> authors = new HashSet<>();**

    public Book() {
    }

    public Book(String title, String isbn) {
        this.title = title;
        this.isbn = isbn;
    }

    public Book(String title, String isbn, Set<Author> authors) {
        this.title = title;
        this.isbn = isbn;
        this.authors = authors;
    }
(...)
}

并且必须创建新的Author.java实体:

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String forename;
    private String surname;

    @ManyToMany(mappedBy = "authors")
    private Set<Book> books = new HashSet<>();

    public Author() {
    }

    public Author(String forename, String surname) {
        this.forename = forename;
        this.surname = surname;
    }

    public Author(String forename, String surname, Set<Book> books) {
        this.forename = forename;
        this.surname = surname;
        this.books = books;
    }
(...)
}

您能指导我现在应该做什么吗?我想在 thymeleaf 中显示作者的书籍列表,但我不知道如何在 index.html 中创建它,以便它显示所有数据。我是否必须更改我的 IndexController(showAll() 方法)?任何建议将不胜感激。

在 Istiaque Hossain 的帖子后进行编辑:

现在它可以工作,但代码看起来不太好(那两个跨度),建议如何使其更好地被欣赏。

index.html

<table>
    <thead>
    <tr>
        <!--        th Number added for numbering purpose-->
        <th> Number</th>
        <th> Author_Name</th>
        <th> Author_Surn</th>
        <th> Title</th>
        <th> ISBN</th>
    </tr>
    </thead>

    <tbody>
    <tr th:if="${books.isEmpty()}">
        <td colspan="3"> No records</td>
    </tr>

    <tr th:each="book, stat : ${books}" th:object="${book}">
        <!--        added for numbering purpose, variable stat is needed for iteration -->
        <td th:text="${stat.count}">1</td>
        <td>
            <span th:each="author : ${book.authors}">
                <span th:utext="${author.forename}" />
            </span>
        </td>
        <td>
            <span th:each="author : ${book.authors}">
                <span th:utext="${author.surname}" />
            </span>
        </td>
        <td th:text="*{title}"> Title</td>
        <td th:text="*{isbn}"> ISBN</td>
    </tr>
    </tbody>
</table>

看起来像这样: enter image description here

最佳答案

无需更改 Controller 。您只需对 html 页面进行少量更改

试试这个代码,它可能会对你有帮助......

       <tr th:each="book, stat : ${books}" th:object="${book}">
                <!--        added for numbering purpose, variable stat is needed for iteration -->
                <td th:text="${stat.count}">1</td>
    ///////////////////////////////////////////////
                <td> 
                    <span th:each="author, iterator1: ${book.authors}" th:remove="tag">
                         <span th:utext="${iterator1.index+1+'. ' + author.forename+' <br/>'}" />
                    </span>
                 </td>
    ////////////////////////////////////
                <td th:text="*{title}"> Title</td>
                <td th:text="*{isbn}"> ISBN</td>
            </tr>

关于java - Spring Boot + Thymeleaf : How to show books and their authors in one list on the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57160977/

相关文章:

java - 如何在Java中更快地读取大文本文件?

java - 在 JPanel 内滚动 JFrame

java - 我可以只启用 Spring Session header 身份验证吗?

css - 将 Materials-UI 添加到 Spring + thymeleaf 元素

html - Spring + thymeleaf html电子邮件模板,如何在邮件中添加html标签?

java - 从方法生成后如何打开PDF文件

java - Servlet 中的 POI HSSF XLS 下载问题 - 新的 XLS 文件与以前的工作表一起下载

java - HTTP 请求等待任务完成

spring-boot - Liquibase似乎正在尝试将空ID插入变更集表

Thymeleaf 聚合不起作用