Thymeleaf each嵌套解决前端一对多查询的问题

问题描述

  今天写代码的时候用到了一个一对多的查询,使用的是springboot和Mybatis,在把查询出来的结果传给前端(使用的是thymeleaf模板引擎)显示的时候遇到了问题,因为后端传过来的数据是集合中套着集合,所以前端应该进行两次循环遍历从而得出我想要的对象属性。先丢我的错误代码:

<tr th:each="dormitory:${dormitories}" th:each="repair:${dormitory.getRepairs()}">
    <td th:text="${dormitory.getDid()}"></td>
    <td th:text="${repair.getInfo()}"></td>
    <td th:text="${repair.getState()}"></td>
    <td th:text="${#dates.format(repair.getDate(),'yyyy-MM-dd')}"></td>
    <td>
        <a class="btn btn-sm btn-primary" th:href="@{/stuFindRepair/}+${repair.getRid()}">查看</a>
        <a class="btn btn-sm btn-primary" th:href="@{/stuRepair/}+${repair.getRid()}">修改</a>
    </td>
</tr>

报错信息:

Caused by: org.attoparser.ParseException: 
(Line = 69, Column = 62) Malformed markup: Attribute "th:each" appears more than once in element

这个错误信息意思是不能同时在一个标签中使用两次"th:each"。

解决办法:

借助其他标签来进行一次遍历然后在当前标签中遍历一次,拿到自己要的对象属性,而标签中刚好span标签没有其他属性,不会页面产生影响,所以就使用它。

 <span th:each="dormitory:${dormitories}" >
      <tr th:each="repair:${dormitory.getRepairs()}">
          <td th:text="${dormitory.getDid()}"></td>
          <td th:text="${repair.getInfo()}"></td>
          <td th:text="${repair.getState()}"></td>
          <td th:text="${#dates.format(repair.getDate(),'yyyy-MM-dd')}"></td>
          <td>
              <a class="btn btn-sm btn-primary" th:href="@{/stuFindRepair/}+${repair.getRid()}">查看</a>
              <a class="btn btn-sm btn-primary" th:href="@{/stuRepair/}+${repair.getRid()}">修改</a>
          </td>
     </tr>
</span>

此致,对今天的遇到的问题和解决办法分享给大家,希望对碰到同样问题的人有所帮助,加油!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值