我在xhtml中使用“c:forEach”,方法如下:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
>
<h:head>
<!--I have tried to incorporate this set inside h:form as well. Still multiple calls-->
<c:set var="currentHistory" value="${itemBean.getParticipantItems(itemBean.history_id, 'borrow')}" />
<c:set value="${fn:length(currentHistory) - 1}" var="sizeOfCurrentHistory" />
</h:head>
<h:form enctype="multipart/form-data" >
<body >
<!--I have tried with dataTable and ui:repeat. Still multiple calls. And used step/begin in foreach-->
<c:forEach items="${currentHistory}" var="history" rendered="${fn:length(currentHistory) > 0}" >
<div >
<h:outputLabel for="itemDescription" value="Item Description:" >
<h:outputText id="itemDescription" value="# {history.itemDescription}" style="margin-left:10px;color:black;"/>
</h:outputLabel>
<br />
<h:outputLabel for="itemModel" value="Item Model:" >
<h:outputText id="itemModel" value="#{history.itemModel}" style="margin-left:10px;color:black;"/>
</h:outputLabel>
<br />
<h:outputLabel value="Item Approved:" >
<h:outputText value="Yes" style="margin-left:10px;color:black;" rendered="${history.approved == 1}"/>
<h:outputText value="No" style="margin-left:10px;color:black;" rendered="${history.approved == 0}"/>
</h:outputLabel>
<br />
<h:outputLabel value="Date Created:" >
<h:outputText value="#{history.dateCreated}" style="margin-left:10px;color:black;"/>
</div>
</c:forEach>
</body>
bean调用代码如下: @Named @SessionScoped 公共类ItemBean扩展AbstractBean实现Serializable {
我介绍了一个测试结果的存在,但是没有用。
public List getParticipantItems(String pid, String which) {
System.out.println("called getParticipantItems");
List result = null;
Session session = null;
Transaction tx = null;
String query = null;
this.itemType = which;
if (this.itemFoundList == null) {
// which_history: 0 = individual, 1 = community
try {
session = hib_session();
tx = session.beginTransaction();
if (this.history_which == 0) {
query = " SELECT itm "
+ " FROM Items itm "
+ " WHERE itm.participant_id = :pid AND itm.itemType = :itype ORDER BY itm.dateCreated ";
} else if (this.history_which == 1) {
query = " SELECT itm "
+ " FROM Participant part, Items itm "
+ " INNER join part.item itm "
+ " WHERE part.communityId = :pid AND itm.itemType = :itype ORDER BY itm.dateCreated ";
} else {
// Later query = "FROM Items WHERE participant_id = :pid and itemType = :it";
}
result = session.createQuery(query)
.setParameter("pid", pid)
.setParameter("itype", which)
.list();
tx.commit();
} catch (Exception e) {
tx.rollback();
System.out.println("Error in getParticipantItems");
Logger.getLogger(ItemBean.class.getName()).log(Level.SEVERE, null, e);
} finally {
tx = null;
session = null;
}
this.itemFoundList = result;
}
if (this.itemFoundList != null) {
return this.itemFoundList;
} else {
return result;
}
}
有人可以帮助我理解为什么foreach中的这个支持bean被多次调用了吗?
感谢。
答案 0 :(得分:3)
使用c:set设置的变量具有特定范围。默认范围是页面范围,这意味着每次访问变量时都会评估变量。这就是多次调用getParticipantItems方法的原因。 其他范围是请求,会话和应用程序:
尝试将c:的范围属性设置为请求:
<c:set scope="request" ... />