无法将值从一个Composite组件复制到另一个JSF

时间:2014-04-28 11:56:33

标签: forms jsf primefaces components

我在尝试获取组件的价值时非常令人沮丧。 我正在尝试做一个我可以重复使用的地址组件,一次用于住宅地址,一次用于邮寄地址。有一个selectOneCheck框,允许用户指定住宅地址与邮政地址相同。我希望将第一个(住宅地址)组件的值复制到第二个(邮政地址)组件的值,然后禁用第二个组件。

这是我的复合组件代码:

<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"/>

以下是邮政地址重新使用的组件:

<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,则将布尔值设置为true。

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

这些组件位于名为registration的相同表单上,没有前置ID。

它大致如下:

<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>

我尽力让我的要求尽可能理解,我希望你们能理解我的追求。请帮帮我。

2 个答案:

答案 0 :(得分:0)

在布尔值复选框的值更改侦听器中,您必须填充您未执行的邮政地址对象。如果邮政地址和住宅地址属于同一类型Address,则必须执行

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

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

希望这会有所帮助。

答案 1 :(得分:0)

问题是我没有完全理解JSF生命周期,我正在更新我的值APPLY_REQUEST_VALUES阶段而不是INVOKE_APPLICATION阶段。同时将邮政地址设置为与住宅地址相同的对象是一个坏主意,因为它最终导致我无法更新任何一个而不影响另一个。

效果更好。

public void sameAsResidentialAddress(ValueChangeEvent event)抛出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());
    }

}