因此,我有一个名为“ StudySet.Java”的对象,它包含一个名为“ Rows.Java”的对象的列表。我试图使用th:each循环表示百里香属植物的行列表,并且每一行都有一个名为“ 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;
    }
}


HTML表格/ Th:每个循环

<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 - Thymeleaf Th:使用Spring MVC的每个无限循环-LMLPHP

更新

看来我的问题发生在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>


另外,您确定StudySetRows数目正确吗?

10-06 05:35