2014-04-28 28 views
0

我在嘗試跨組件獲取值時非常沮喪。 我正在嘗試做一個地址組件,我可以重複使用,一次用於住宅地址,一次用於郵政地址。有一個selectOneCheck框允許用戶指定居住地址與郵政地址相同。我想將第一個(居住地址)組件的值複製到第二個(郵政地址)組件的值中,然後禁用第二個組件。無法將值從一個複合組件複製到另一個JSF

這裏是我的複合組件代碼:

<composite:interface> 




    <composite:attribute name="addressLine1" required="true" 
     targets="addressLine1Text" /> 
    <composite:attribute name="addressLine2" required="false" 
     targets="addressLine2Text" /> 
    <composite:attribute name="addressLine3" required="false" 
     targets="addressLine3Text" /> 
    <composite:attribute name="city" required="true" targets="cityText" /> 
    <composite:attribute name="state" required="true" targets="stateText" /> 
    <composite:attribute name="postCode" required="true" 
     targets="postCodeText" /> 
    <composite:attribute name="styleClass" required="true" /> 

    <composite:attribute name="disabled" required="true" /> 

    <composite:attribute name="addressLine1Label" required="false" 
     targets="addressLine1Label" /> 
    <composite:attribute name="addressLine2Label" required="false" 
     targets="addressLine2Label" /> 
    <composite:attribute name="addressLine3Label" required="false" 
     targets="addressLine3Label" /> 
    <composite:attribute name="cityLabel" required="false" 
     targets="cityLabel" /> 
    <composite:attribute name="stateLabel" required="false" 
     targets="stateLabel" /> 
    <composite:attribute name="postCodeLabel" required="false" 
     targets="postCodeLabel" /> 


<composite:implementation> 
<composite:renderFacet name="heading"/> 

    <h:panelGrid columns="3"> 
     <h:outputLabel id="addressLine1Label" for="addressLine1Text" 
      value="#{cc.attrs.addressLine1Label}" /> 
     <h:inputText id="addressLine1Text" value="#{cc.attrs.addressLine1}" > 
     </h:inputText> 
     <h:message for="addressLine1Text" id="addressLine1Message" /> 


     <h:outputLabel id="addressLine2Label" for="addressLine2Text" 
      value="#{cc.attrs.addressLine2Label}" /> 
     <h:inputText id="addressLine2Text" value="#{cc.attrs.addressLine2}"> 
     </h:inputText> 
     <h:message for="addressLine2Text" id="addressLine2Message" /> 



     <h:outputLabel id="addressLine3Label" for="addressLine3Text" 
      value="#{cc.attrs.addressLine3Label}" /> 
     <h:inputText id="addressLine3Text" value="#{cc.attrs.addressLine3}"> 
     </h:inputText> 
     <h:message for="addressLine3Text" id="addressLine3Message" /> 



     <h:outputLabel id="cityLabel" for="cityText" 
      value="#{cc.attrs.cityLabel}" /> 
     <h:inputText id="cityText" value="#{cc.attrs.city}"> 
     </h:inputText> 
     <h:message for="cityText" id="cityMessage" /> 


     <h:outputLabel id="stateLabel" for="stateText" 
      value="#{cc.attrs.stateLabel}" /> 
     <h:inputText id="stateText" value="#{cc.attrs.state}"> 
     </h:inputText> 
     <h:message for="stateText" id="stateMessage" /> 


     <h:outputLabel id="postCodeLabel" for="postCodeText" 
      value="#{cc.attrs.postCodeLabel}" /> 
     <h:inputText id="postCodeText" value="#{cc.attrs.postCode}"> 
     </h:inputText> 
     <h:message for="postCodeText" id="postCodeMessage" /> 

    </h:panelGrid> 
</composite:implementation> 

這裏是正在使用的組件:

<add:address 
         id="residentialAddress" styleClass="bold" 
         addressLine1="#{registrationBean.residentialAddress.addressLine1}" 
         addressLine2="#{registrationBean.residentialAddress.addressLine2}" 
         addressLine3="#{registrationBean.residentialAddress.addressLine3}" 
         city="#{registrationBean.residentialAddress.city}" 
         state="#{registrationBean.residentialAddress.state}" 
         postCode="#{registrationBean.residentialAddress.postCode}" 
         addressLine1Label="#{msgs.addressLine1}" 
         addressLine2Label="#{msgs.addressLine2}" 
         addressLine3Label="#{msgs.addressLine3}" 
         cityLabel="#{msgs.city}" 
         stateLabel="#{msgs.state}" 
         postCodeLabel="#{msgs.postCode}"  
         > 

         <f:facet name="heading"> 
          <h:outputLabel value="#{msgs.residentialAddress}" /> 
         </f:facet> 

          <f:converter for="add" converterId="myConvertor"/> 

        </add:address> 

這裏是selectOneRadioButton:

     <p:selectBooleanCheckbox 
         id="sameAsResidentialAddress" 
         itemLabel="#{msgs.sameAsResidentialAddress}" 
         valueChangeListener="#{registrationBean.sameAsResidentialAddress}" 

          > 
        <f:ajax execute="@form" render="postalAddress"> 

        </f:ajax> 
        </p:selectBooleanCheckbox> 
        <h:message for="sameAsResidentialAddress"/> 

這裏是元件被resused郵政地址:

<add:address id="postalAddress" styleClass="bold" 
         addressLine1="#{registrationBean.residentialAddress.addressLine1}" 
         addressLine2="#{registrationBean.postalAddress.addressLine2}" 
         addressLine3="#{registrationBean.postalAddress.addressLine3}" 
         city="#{registrationBean.postalAddress.city}" 
         state="#{registrationBean.postalAddress.state}" 
         postCode="#{registrationBean.postalAddress.postCode}" 
         addressLine1Label="#{msgs.addressLine1}" 
         addressLine2Label="#{msgs.addressLine2}" 
         addressLine3Label="#{msgs.addressLine3}" cityLabel="#{msgs.city}" 
         stateLabel="#{msgs.state}" postCodeLabel="#{msgs.postCode}" 
         rendered="#{!registrationBean.same}"> 

         <f:facet name="heading"> 
          <h:outputLabel value="#{msgs.postalAddress}" /> 
         </f:facet> 


         <f:converter for="add" converterId="myConvertor"/> 



        </add:address> 

Java代碼是一個用於POJO的地址和登記。註冊有一個valueChangeListener,如果選中sameAsResidentialAddress selectOneRadio,則將boolean設置爲true。

public void sameAsResidentialAddress(ValueChangeEvent event){ 
     setSame((Boolean)event.getNewValue()); 
    } 

這些組件在名稱相同的表單上註冊時沒有給它們預先標識。

它大致是這樣的:

<h:form id="Registration"> 
    <p:wizard> 
    <p:tab id ="address" title="Your Addresses"> 
    <p:panel id="addresses"> 
    <add:address id= "residentialAddress"> 
     ... 
    </add:address> 

     <p:selectBooleanCheckbox id="sameAsResidentialAddress"> 
     ... 
     </p:selectBooleanCheckbox> 

    <add:address id= "postalAddress"> 
     ... 
    </add:address>  
    </p:panel> 
    </p:tab> 
    </p:wizard> 
</h:form> 

我已經盡了最大的努力使我的要求是可以理解的可能,我希望你們能理解我所追求的。請幫幫我。

+0

在值更改偵聽器中,您必須填充postalAddress對象。如果郵政地址和住宅地址都是相同類型的地址,那麼你可以這樣做 –

回答

0

問題是我沒有完全理解JSF生命週期,我正在更新我的值APPLY_REQUEST_VALUES階段而不是INVOKE_APPLICATION階段。同時將郵政地址設置爲與居住地址相同的對象是一個壞主意,因爲它最終導致我無法在不影響其他人的情況下更新任何一個人。

這個效果更好。

public void sameAsResidentialAddress(ValueChangeEvent event)throws AbortProcessingException FacesContext context = FacesContext.getCurrentInstance();

if(context.getCurrentPhaseId() != PhaseId.INVOKE_APPLICATION){ 
     event.setPhaseId(PhaseId.INVOKE_APPLICATION); 
     event.queue(); 
     return; 
    } 
    if ((Boolean)event.getNewValue()){ 

     this.setSame(true); 
     this.getPostalAddress().setAddressLine1(getResidentialAddress().getAddressLine1()); 
     this.getPostalAddress().setAddressLine2(getResidentialAddress().getAddressLine2()); 
     this.getPostalAddress().setAddressLine3(getResidentialAddress().getAddressLine3()); 
     this.getPostalAddress().setCity(getResidentialAddress().getCity()); 
     this.getPostalAddress().setState(getResidentialAddress().getState()); 
     this.getPostalAddress().setPostCode(getResidentialAddress().getPostCode()); 
     System.out.println("Value was true"); 
     System.out.println("Postal Address " + getPostalAddress()); 
     System.out.println("Residential Address " + getResidentialAddress()); 
    }else{ 
     setPostalAddress(new Address()); 
    } 

} 
0

在布爾值複選框的值更改偵聽器中,您必須填寫您未執行的郵政地址對象。如果同時郵寄地址和住址是同一類型Address,那麼你所要做的

public void sameAsResidentialAddress(ValueChangeEvent event){ 
    if((Boolean)event.getNewValue()) { 

     setSame((Boolean)event.getNewValue()); 
     registrationBean.setPostalAddress(registrationBean.getResidentialAddress()); 
    } 
} 

希望這會有所幫助。

+0

謝謝你Srikanth Ganji,你回答有點幫助,但不完全。我會發布我的答案,所以你可以確保它。 – user1279734

相關問題