关于thymeleaf遍历一对多的问题!

Public class Auction{
private int auctionid;
Private list<Auctionrecord> 
auctionrecord List;
}


Public class Auctionrecord{
Private Users users;}



Public class Users{
Private int id;
Private string name;
}

controller 返回很多个Auction对象,然后auction里面的auctionrecord的user怎么在 th:each遍历出来

基于Monster 组和GPT的调写:

用Thymeleaf的嵌套th:each语法来遍历一对多关系的数据

<table>
  <thead>
    <tr>
      <th>Auction ID</th>
      <th>User ID</th>
      <th>User Name</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="auction : ${auctions}">
      <td th:text="${auction.auctionid}"></td>
      <td></td>
      <td></td>
      <td>
        <table>
          <tbody>
            <tr th:each="auctionRecord : ${auction.auctionrecordList}">
              <td></td>
              <td th:text="${auctionRecord.users.id}"></td>
              <td th:text="${auctionRecord.users.name}"></td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>



  • 用了两个嵌套的th:each语法,第一个用来遍历Auction对象列表,第二个用来遍历每个Auction对象中的Auctionrecord列表。在内层循环中通过auctionRecord对象的users属性来获取对应的用户信息。