Thymeleaf Th:每个无限循环,使用Spring MVC [英] Thymeleaf Th:Each infinite loop, with Spring MVC

查看:51
本文介绍了Thymeleaf Th:每个无限循环,使用Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我会贴出我的控制器的一些代码和我的 html 页面,如果有人能看到我哪里出错了,那就太好了.提前致谢,如果有人想查看更多代码,请告诉我.

控制器

@Controller公共类 StudySetController {私人 StudySetRepository studySetRepo;@RequestMapping(value = "studySet/{studySetId}", method = RequestMethod.GET)public String addPostGet(@PathVariable Long studySetId, ModelMap 模型) {StudySet studySet = studySetRepo.findOne(studySetId);model.put("studySet", studySet);列表<行>行 = studySet.getRows();模型.put("行",行);返回学习集";}@自动连线public void studySetRepo(StudySetRepository studySetRepo) {this.studySetRepo = studySetRepo;}}

Html Table/Th:Each Loop

<div class="col-md-5 col-centric" style="padding-top:50px;"><div class="panel panel-default user-form"><div class="panel-heading"><h4><span th:text="${studySet.title}"></span></h4>

<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>

<div th:if="${#lists.isEmpty(rows)}"><div style="float: left;">没有要显示的行.<br/>

这也是我的实际页面的图片,你无法看到所有内容,但列表会持续很长时间,我有两行分配给这个 studySet,这些行只是用虚拟信息重复.

更新

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

解决方案

我的问题是在我的域对象中 Rows 作为 List 返回到 StudySet,我不知道为什么,但这会导致循环,一旦我将它切换到 HashSet,我的问题就解决了.

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.

解决方案

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.

这篇关于Thymeleaf Th:每个无限循环,使用Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆