【问题标题】:Cannot copy values from one Composite component to another JSF无法将值从一个复合组件复制到另一个 JSF
【发布时间】:2014-04-28 11:56:32
【问题描述】:

我在尝试跨组件获取值时非常沮丧。 我正在尝试做一个可以重复使用的地址组件,一次用于住宅地址,一次用于邮政地址。有一个 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。如果选中了 sameAsResidentialAddress selectOneRadio,则注册有一个 valueChangeListener 将布尔值设置为 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>

我已尽力使我的要求尽可能容易理解,我希望你们能理解我所追求的。请帮帮我。

【问题讨论】:

  • 在值更改侦听器中,您必须填充 postalAddress 对象。如果邮政地址和住宅地址都属于同一类型的地址,那么您可以这样做

标签: forms jsf primefaces components


【解决方案1】:

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

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

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

希望这会有所帮助。

【讨论】:

  • 谢谢 Srikanth Ganji,你的回答有点帮助但不完全。我会发布我的答案,所以你也可以确保它。
【解决方案2】:

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

这样效果更好。

public void sameAsResidentialAddress(ValueChangeEvent 事件) 抛出 AbortProcessingException{ FacesContext 上下文 = 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());
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多