Thymeleaf Th:Each infinite loop, with Spring MVC

£可爱£侵袭症+ 提交于 2021-01-28 00:35:34

问题


So I have an object called "StudySet.Java" and it contains a list of objects called "Rows.Java". I'm trying to represent the list of rows from thymeleaf using a th:each loop, and each row has a string named "question", and a string named "answer". However whenever I try to represent the list by getting the rows from that studySet, and adding it to the model, there is an infinite loop of question and answers.

I'll put up some code of my controller, and my html page, and if anyone can see where I'm going wrong that would be great. Thanks in advance and if anyone would like to see more code just let me know.

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>

Here's also a picture of my actual page, you can't see everything but the list goes on for a long time, and I have two rows assigned to this studySet that are just repeating with dummy information.

UPDATE

It appears that my issue is happening on the Java side, because when I debug, the two rows that are assigned to the study set are just repeating. However I have no idea why this is happening.


回答1:


Try to change:

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

to:

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

Also, are you sure, that StudySet has correct number of Rows?




回答2:


My problem was that in my domain object Rows was returning to StudySet as a List, and I'm not sure why but that was causing the loop, as soon as I switched it to HashSet my problem was solved.



来源:https://stackoverflow.com/questions/35140272/thymeleaf-theach-infinite-loop-with-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!