gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:24:24 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com