2016-07-26 57 views
0

我有一個項目的集合,每個項目都有最小值,最大值和最大值。有驗證器可確保最大值大於最小值等。驗證器使用綁定,以便比較用戶輸入的所有三個值。最初他們是在用戶界面:重複,但這是一個下拉的麻煩,我發現切換到C:forEach修復它。不幸的是,它也搞亂了綁定。我希望有一種方法可以解決它們而不會回到ui上:重複。c中的綁定:forEach

是否可以在c:forEach中綁定?有沒有辦法在綁定中添加一個id或一個varStatus索引,因此列表中的每個項目都不相同?例如在下面的例子中,我嘗試過的東西,如

<h:inputText id="impactMin_#{impact.id}" binding="#{impactMin_#{impact.id}}"> 
<h:inputText id="impactMin_#{impact.id}" binding="#{impactMin}_#{impact.id}"> 

但不出所料,那些不起作用。如果有人知道這樣做的明智方式,我會非常感激聽到它。

這是代碼的簡化版本:

<c:forEach id="impacts" var="impact" items="#{mybean.impacts}" varStatus="i"> 
    <h:panelGroup layout="block" id="impactContainer_#{impact.id}"> 
     <h:outputText value="#{impact.score_impact_type_idInterface.score_impact_type_name}"/> 
     <h:panelGroup layout="block" class="row form-group" id="impactAssessment_#{impact.id}"> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMin" value="#{impact.current_impact_min}" binding="#{impactMin}" label="Minimum"> 
        <f:attribute name="impactMl" value="#{impactMl}"/> 
        <f:attribute name="impactMax" value="#{impactMax}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactMinValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMl" value="#{impact.current_impact_ml}" binding="#{impactMl}" label="Most likely"> 
        <f:attribute name="impactMin" value="#{impactMin}"/> 
        <f:attribute name="impactMax" value="#{impactMax}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactLikelyValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMax" value="#{impact.current_impact_max}" binding="#{impactMax}" label="Maximum"> 
        <f:attribute name="impactMin" value="#{impactMin}"/> 
        <f:attribute name="impactMl" value="#{impactMl}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactMaxValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <div class="col-sm-12"> 
       <h:message for="impactMin"/> 
       <h:message for="impactMl"/> 
       <h:message for="impactMax"/> 
      </div> 
     </h:panelGroup> 
    </h:panelGroup> 
</c:forEach> 

回答

0

在試圖更好地瞭解在通用綁定的,我發現這個答案JSF 2 composites and binding for validation。雖然不完全一樣的東西,我發現它可以適用於C:靠近頂部PARAM位:的forEach這樣的:

<c:forEach id="impacts" var="impact" items="#{mybean.impacts}" varStatus="i"> 
    <h:panelGroup layout="block" id="impactContainer_#{impact.id}"> 
     <h:outputText value="#{impact.score_impact_type_idInterface.score_impact_type_name}"/> 
     <h:panelGroup layout="block" class="row form-group" id="impactAssessment_#{impact.id}"> 
      <ui:param name="impactMin" value="impactMin_#{impact.id}" /> 
      <ui:param name="impactMl" value="impactMl_#{impact.id}" /> 
      <ui:param name="impactMax" value="impactMax_#{impact.id}" /> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMin_#{impact.id}" value="#{impact.current_impact_min}" binding="#{requestScope[impactMin]}" label="Minimum"> 
        <f:attribute name="impactMl" value="#{requestScope[impactMl]}"/> 
        <f:attribute name="impactMax" value="#{requestScope[impactMax]}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactMinValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMl_#{impact.id}" value="#{impact.current_impact_ml}" binding="#{requestScope[impactMl]}" label="Most likely"> 
        <f:attribute name="impactMin" value="#{requestScope[impactMin]}"/> 
        <f:attribute name="impactMax" value="#{requestScope[impactMax]}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactLikelyValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <h:panelGroup layout="block"> 
       <h:inputText id="impactMax_#{impact.id}" value="#{impact.current_impact_max}" binding="#{requestScope[impactMax]}" label="Maximum"> 
        <f:attribute name="impactMin" value="#{requestScope[impactMin]}"/> 
        <f:attribute name="impactMl" value="#{requestScope[impactMl]}"/> 
        <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" /> 
        <f:validator binding="#{impactMaxValidator}"/> 
       </h:inputText> 
      </h:panelGroup> 
      <div class="col-sm-12"> 
       <h:message for="impactMin_#{impact.id}"/> 
       <h:message for="impactMl_#{impact.id}"/> 
       <h:message for="impactMax_#{impact.id}"/> 
      </div> 
     </h:panelGroup> 
    </h:panelGroup> 
</c:forEach> 

注意的UI。這似乎是訣竅。感謝BalusC!