本文介绍了多个相同长度的模型同时迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个关于 spring boot 中的 thymeleaf 的非常特别的问题,使用 Java Persitence API (JPA).我有三个表(A、B、C),因此模型看起来像这样:答:@Entity@Table(name = "A")公共类 ATable {私人援助;私人字符串内容;//getter 和 setter}乙:@Entity@Table(name = "B")公共类 BTable {私人国际投标;私人字符串 bcontent;//getter 和 setter}C:@Entity@Table(name = "C")公共类 CTable {私有 int cid;私人字符串 ccontent;//getter 和 setter}按照设计,这三个表格将具有相同数量的内容.表格的行是这样的:答:帮助内容1 a12 a23 a3乙:出价内容1 b12 b23 b3C:cid ccontent1 c12 c23 c3现在,我想在我的 HTML5 模板中同时遍历这三个表,结果应该是这样的:a1b1c1b1b2b3c1c2c3我的控制器是这样的:...列表atable = new ArrayList();model.addAttribute(atable", atable);列表atables = atableRepository.findAll();model.addAttribute(atables", atables);列表btable = new ArrayList();model.addAttribute(btable", btable);列表btables = btableRepository.findAll();model.addAttribute(btables", btables);列表ctable = new ArrayList();model.addAttribute(ctable", ctable);列表ctables = ctableRepository.findAll();model.addAttribute(ctables", ctables);...为了只遍历这些表中的一个,我可以这样做:<tr th:each=atable : ${atables}"><span th:text="${atable.acontent}"></tr>但是我们怎样才能同时使用三个表呢?我想我已经很清楚地表达了我的问题,我真的希望有人能帮助我.提前致谢. 解决方案 创建一个 DTO 来表示 HTML 中的一行:公共类行{私人 ATable 餐桌;私人 BTable btable;私有 CTable 表;//构造函数 + getter 在这里}然后在您的控制器中使用它:@GetMapping公共字符串showPage(模型模型){列表atables = atableRepository.findAll();列表btables = btableRepository.findAll();列表ctables = ctableRepository.findAll();列表行 = 新的 ArrayList();for( int i = 0; i < atables.size();i++) {rows.add(new Row(atables.get(i), btables.get(i), ctables.get(i));}model.addAttribute("rows", rows);}现在在 Thymeleaf 中遍历您的行:<span th:text="${row.atable.acontent}"><span th:text="${row.btable.acontent}"><span th:text="${row.ctable.acontent}"></tr>I have a very particular question related to thymeleaf in spring boot, using the Java Persitence API (JPA).I have three tables (A, B, C), hence models, they look like this:A:@Entity@Table(name = "A")public class ATable { private int aid; private String acontent; // getters and setters}B:@Entity@Table(name = "B")public class BTable { private int bid; private String bcontent; // getters and setters}C:@Entity@Table(name = "C")public class CTable { private int cid; private String ccontent; // getters and setters}These three tables, by design, will have the same amount of content.The rows of the tables are like this:A:aid acontent1 a12 a23 a3B:bid bcontent1 b12 b23 b3C:cid ccontent1 c12 c23 c3Now, I would like to iterate through these three tables simultaneously in my HTML5 template, the result should be something like this:a1b1c1b1b2b3c1c2c3My controller is like this:...List<ATable> atable = new ArrayList<>();model.addAttribute("atable", atable);List<ATable> atables = atableRepository.findAll();model.addAttribute("atables", atables);List<BTable> btable = new ArrayList<>();model.addAttribute("btable", btable);List<BTable> btables = btableRepository.findAll();model.addAttribute("btables", btables);List<CTable> ctable = new ArrayList<>();model.addAttribute("ctable", ctable);List<CTable> ctables = ctableRepository.findAll();model.addAttribute("ctables", ctables);...To iterate through only one of these tables I could something like this:<tr th:each="atable : ${atables}"><span th:text="${atable.acontent}"></tr>But how can we do it simultaneously with, say, three tables?I think I have been clear enough with tables representing my issue, and I really hope someone can help me. Thanks in advance. 解决方案 Create a DTO that represents a row in your HTML:public class Row { private ATable atable; private BTable btable; private CTable ctable; // constructor + getters here}Then use this in your controller:@GetMappingpublic String showPage(Model model) { List<ATable> atables = atableRepository.findAll(); List<BTable> btables = btableRepository.findAll(); List<CTable> ctables = ctableRepository.findAll(); List<Row> rows = new ArrayList<>(); for( int i = 0; i < atables.size();i++) { rows.add(new Row(atables.get(i), btables.get(i), ctables.get(i)); } model.addAttribute("rows", rows);}Now iterate in Thymeleaf through your rows:<tr th:each="row : ${rows}"> <span th:text="${row.atable.acontent}"> <span th:text="${row.btable.acontent}"> <span th:text="${row.ctable.acontent}"></tr> 这篇关于多个相同长度的模型同时迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 06:43