多次调用JSF getter方法

多次调用JSF getter方法,jsf,getter,managed-bean,Jsf,Getter,Managed Bean,我以以下方式在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"

我以以下方式在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>
}

有人能帮我理解为什么foreach中的这个支持bean被多次调用吗


谢谢。

使用c:set设置的变量具有特定的范围。默认范围是页面范围,这意味着每次访问变量时都会对其求值。这就是为什么会多次调用GetParticipanItems方法。 其他范围包括请求、会话和应用程序:

  • 请求范围:为每个请求计算变量
  • 会话范围:每个会话对变量求值一次
  • 应用范围:该变量只计算一次,并且对于每个客户端都是相同的
尝试将c:的范围属性设置为请求:

    <c:set scope="request" ... />


谢谢,Alex Fire。我将范围设置为请求。很有魅力,不客气。接受答案怎么样,嗯?
    <c:set scope="request" ... />