gpt4 book ai didi

java - 连接 :each with Thymeleaf 中对象的字符串

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

我有一个服务休息(使用 javaEe、Spring 和 thymeleaf),他向我发送了一本具有以下形状的对象书:

 public class BookDto   {
@JsonProperty("id")
private Long id;

@JsonProperty("isbn")
private String isbn;

@JsonProperty("title")
private String title;

@JsonProperty("authors")
@Valid
private List<BooksAuthors> authors;
...

我需要从 BooksAuthors 对象列表中提取作者姓名,如下所示:

public class BooksAuthors   {
@JsonProperty("firstName")
private String firstName;

@JsonProperty("lastName")
private String lastName;
...

我想在表格中显示有关作者的信息,但我不知道如何使用 Thymeleaf 来做到这一点。我认为这将是一个 th:each 来解析作者,在一个 th:each 中来解析书籍,但我无法让它工作。我想要的结果是

                <table class="table">
<thead>
<tr>
<th scope="col">Titre</th>
<th scope="col">Auteur</th>
<th scope="col">isbn</th>
<th scope="col">Exemplaires disponibles</th>
</tr>
</thead>
<tbody>
<tr th:each="book:${books}">
<td th:text="${book.getTitle()}"></td>
<td>Author1, Author2</td>
<td th:text="${book.getIsbn()}"></td>
<td>Pas implémenté</td>
</tr>
</tbody>
</table>

我设法得到一个结果。我尝试创建一个 var 并将其连接起来,但我仍然需要删除最后一个“,”。更重要的是,它在我看来非常笨拙。

<td>
<span th:each="author:${book.getAuthors()}"
th:with="authorName=${author.getFirstName()} + ' ' + ${author.getLastName()} + ', '"
th:text="${authorName}">
</span>
</td>

我想知道是否有更好、更简单的解决方案?或者我应该在我的业务层而不是 thymeleaf 中进行?非常感谢!

最佳答案

我想你可以为此使用迭代状态对象:

https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

制作这样的东西:

<span th:each="author, iterStat:${book.getAuthors()}"
th:with="authorName=${author.getFirstName()} + ' ' + ${author.getLastName() + ${iterStat.count != iterStat.size ? ',' : ''}}"
th:text="${authorName}">
</span>

关于java - 连接 :each with Thymeleaf 中对象的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58842811/

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