有2个实体,A和B,是一对多的关系,A中有属性Set<B> items=new HashSet<B>(),B中有属性A a=new A();数据持久化我用的是hibernate;
我想在JSP中把所有A的属性都列出来,包括Set<B> items中的第一个元素的id,这个值怎么取?
(A我已经保存在request中了) request.setAttribute("as", as);
<c:forEach items="${as}" var="a" >
<td>${a.id}</td>
<td>${a.items.id}</td> (这样写肯定是不对的)
</c:forEach>
如果Set<B>该成List<B>也可以
我想在JSP中把所有A的属性都列出来,包括Set<B> items中的第一个元素的id,这个值怎么取?
(A我已经保存在request中了) request.setAttribute("as", as);
<c:forEach items="${as}" var="a" >
<td>${a.id}</td>
<td>${a.items.id}</td> (这样写肯定是不对的)
</c:forEach>
如果Set<B>该成List<B>也可以
<td>${a.id} </td>
<td>${a.items.id} </td> (这样写肯定是不对的)
</c:forEach>
如果Set <B>该成List <B>也可以这里a就是1个a对象 a对象中items 是b对象的集合
${a.items}就是1个a对应b的对象集合。你在循环下应该就可以了。
<td>${a.id} </td>
<td>
<c:forEach items="${a.items}" var="b" >
${b.id}
</c:forEach>
</td>
</c:forEach>
<td>${a.id} </td>
<td>${a.bid}</td>
</c:forEach>因为此时,bid已成为一个属性了。
<c:forEach items="${as}" var="a" >
<td>${a.id} </td>
<td>${a.items[0].id}</td><!--修改处-->
</c:forEach>