代码之家  ›  专栏  ›  技术社区  ›  Parris

如何在jsp中遍历集合?(休眠关联)

  •  6
  • Parris  · 技术社区  · 16 年前

    所以我对jsp还很陌生。我试过几种方法。在php或automagicy框架中有意义的方法…我可能想得太多了…

    我有冬眠一对多关联。也就是说X类有许多Y类。在X类的view.jsp中。我想获取所有类y,其中y的外键与x的主键匹配并显示它们。看起来hibernate把这些东西放进了一个集合。现在,问题是如何遍历这个集合,然后输出它的内容…

    我有点被难住了。我试着写剧本,

    <%
    java.util.Iterator iter = aBean.getYs().iter(); // aBeans is the bean name
    // getYs would return the set and iter would return an iterator for the set
    while(iter.hasNext) { 
       model.X a = new iter.next() 
    %>
       <h1><%=a.getTitle()%></h1>
    <%
    }
    %>
    

    这样的事情应该管用吗?HMM

    1 回复  |  直到 16 年前
        1
  •  10
  •   Bozho    16 年前

    您最好将bean作为请求(或会话)属性放置,并使用jstl对其进行迭代:

    <c:forEach items="${bean.ys}" var="item">
       <h1>${item.title}</h1>
    </c:forEach>
    
    推荐文章