如何在JSP中遍历Set? (休眠协会) [英] How can I go through a Set in JSP? (Hibernate Associations)

查看:71
本文介绍了如何在JSP中遍历Set? (休眠协会)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对JSP还是陌生的.我已经尝试了几种方法.在PHP或automagicy框架中有意义的方法...实际上我可能考虑的太多了...

So I am pretty new to JSP. I have tried this a few ways. Ways that would make sense in PHP or automagicy frameworks... I am probably thinking too much in fact...

我有一个休眠的一对多关联.也就是说,x类具有许多y类.在x类的view.jsp中.我想获取y的所有类,其中y的外键与x的主键匹配并显示它们.休眠似乎将这些东西适当地放入了一个集合.现在,问题是我该如何遍历此集合,然后输出其内容...

I have a hibernate one to many association. That is class x has many of class y. In class x's view.jsp. I would like to grab all of class y, where the foreign key of y matches the primary key of x and display them. It seems that hibernate properly puts this stuff into a set. Now, the question is how can I iterate through this set and then output it's contents...

我有点被困在这里.我试图写一个脚本,

I am kind of stumped here. I tried to write a scriptlet,

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

似乎这种事情应该起作用?嗯嗯嗯

It would seem that that sort of thing should work? Hmmmmmm

推荐答案

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

You'd better place the bean as request (or session) attribute and iterate over it using JSTL:

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

这篇关于如何在JSP中遍历Set? (休眠协会)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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