Thymeleaf Th:Each 无限循环,带有 Spring MVC
Thymeleaf Th:Each infinite loop, with Spring MVC
所以我有一个名为 "StudySet.Java" 的对象,它包含一个名为 "Rows.Java" 的对象列表。我正在尝试使用 th:each 循环表示 thymeleaf 中的行列表,每行都有一个名为 "question" 的字符串和一个名为 "answer" 的字符串。但是,每当我尝试通过从该 studySet 中获取行并将其添加到模型中来表示列表时,就会出现无限循环的问题和答案。
我将发布我的控制器的一些代码,以及我的 html 页面,如果有人能看到我哪里出错了,那就太好了。提前致谢,如果有人想查看更多代码,请告诉我。
控制器
@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;
}
}
HtmlTable/Th:Each循环
<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,它们只是重复虚拟信息。
更新
看来我的问题出现在 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
的 Rows
数量正确吗?
我的问题是,在我的域中,对象 Rows
作为 List
返回到 StudySet
,我不确定为什么会导致循环,尽快当我把它切换到 HashSet
时,我的问题就解决了。
所以我有一个名为 "StudySet.Java" 的对象,它包含一个名为 "Rows.Java" 的对象列表。我正在尝试使用 th:each 循环表示 thymeleaf 中的行列表,每行都有一个名为 "question" 的字符串和一个名为 "answer" 的字符串。但是,每当我尝试通过从该 studySet 中获取行并将其添加到模型中来表示列表时,就会出现无限循环的问题和答案。
我将发布我的控制器的一些代码,以及我的 html 页面,如果有人能看到我哪里出错了,那就太好了。提前致谢,如果有人想查看更多代码,请告诉我。
控制器
@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;
}
}
HtmlTable/Th:Each循环
<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,它们只是重复虚拟信息。
更新
看来我的问题出现在 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
的 Rows
数量正确吗?
我的问题是,在我的域中,对象 Rows
作为 List
返回到 StudySet
,我不确定为什么会导致循环,尽快当我把它切换到 HashSet
时,我的问题就解决了。