dochsner
dochsner

Reputation: 249

Thymeleaf Th:Each infinite loop, with Spring MVC

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.

enter image description here

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.

Upvotes: 2

Views: 2639

Answers (2)

dochsner
dochsner

Reputation: 249

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.

Upvotes: 0

m.aibin
m.aibin

Reputation: 3603

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?

Upvotes: 1

Related Questions