gpt4 book ai didi

java - Thymeleaf Th :Each infinite loop, 与 Spring MVC

转载 作者:行者123 更新时间:2023-11-30 10:50:18 28 4
gpt4 key购买 nike

所以我有一个名为“StudySet.Java”的对象,它包含一个名为“Rows.Java”的对象列表。我正在尝试使用 th:each 循环表示 thymeleaf 中的行列表,每行都有一个名为“question”的字符串和一个名为“answer”的字符串。但是,每当我尝试通过从该 studySet 中获取行并将其添加到模型中来表示列表时,就会出现无限循环的问题和答案。

我将提供我的 Controller 的一些代码和我的 html 页面,如果有人能看到我哪里出错了,那就太好了。提前致谢,如果有人想查看更多代码,请告诉我。

Controller

@Controller
public class StudySetController {

private StudySetRepository studySetRepo;

@RequestMapping(value = "studySet/{studySetId}", method = RequestMethod.GET)
public String addPostGet(@PathVariable Long studySetId, ModelMap model) {
StudySet studySet = studySetRepo.findOne(studySetId);
model.put("studySet", studySet);
List<Row> rows = studySet.getRows();
model.put("rows", rows);

return "studySet";
}

@Autowired
public void studySetRepo(StudySetRepository studySetRepo) {
this.studySetRepo = studySetRepo;
}
}

Html Table/Th:Each Loop

<div class="row row-centered">
<div class="col-md-5 col-centered" style="padding-top:50px;">
<div class="panel panel-default user-form">
<div class="panel-heading">
<h4><span th:text="${studySet.title}"></span></h4>
</div>
<div class="panel-body">
<table class="table table-bordered">
<tr th:each="row : *{rows}" th:object="${row}">
<td>
<p><span th:text="${row.answer}"></span></p>
<p><span th:text="${row.question}"></span></p>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>

<div th:if="${#lists.isEmpty(rows)}">
<div style="float: left;">
There are no rows to display.<br/>
</div>
</div>

这也是我的实际页面的图片,您看不到所有内容,但列表会持续很长时间,并且我有两行分配给这个 studySet,它们只是重复虚拟信息。

enter image description here

更新

看来我的问题发生在 Java 端,因为当我调试时,分配给学习集的两行只是重复。但是我不知道为什么会这样。

最佳答案

尝试改变:

<tr th:each="row : *{rows}" th:object="${row}">

到:

<tr th:each="r : ${rows}">
<td>
<p><span th:text="${r.answer}"></span></p>
<p><span th:text="${r.question}"></span></p>
</td>
</tr>

此外,您确定StudySet行数 正确吗?

关于java - Thymeleaf Th :Each infinite loop, 与 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35140272/

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