我在尝试跨组件获取值时遇到了一段非常令人沮丧的时间。我正在尝试做一个可以重复使用的地址组件,一次用于住宅地址,一次用作邮政地址。有一个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。Registration有一个值ChangeListener,如果选中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>
我已经尽我所能让我的请求尽可能地被理解,我希望你们能理解我在追求什么。请帮帮我。
在布尔复选框的值更改侦听器中,您必须填充您没有执行的邮政地址对象。如果邮寄地址和居住地址都是相同类型的Address
,那么你必须进行
public void sameAsResidentialAddress(ValueChangeEvent event){
if((Boolean)event.getNewValue()) {
setSame((Boolean)event.getNewValue());
registrationBean.setPostalAddress(registrationBean.getResidentialAddress());
}
}
希望这会有所帮助。
问题是我没有完全理解JSF的生命周期,我在APPLY_REQUEST_values阶段而不是INVOKE_APPLICATION阶段更新我的值。此外,将邮政地址设置为与住宅地址相同的对象也是一个坏主意,因为这最终导致我无法在不影响另一个的情况下更新其中一个。
这样效果更好。
public void sameAsResidentialAddress(ValueChangeEvent事件)引发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());
}
}