我的 JSF 页面出现问题,我在第一个计费名字字段中输入了一个名字。如果我点击复制账单地址复选框;这将停止呈现送货地址面板并通过 ajax 将其隐藏,我刚刚在第一个字段中键入的值将重置为之前的状态。
JSF 页面
<h:form>
<p:inputText value="#{addressBean.billingAddress.firstName}" required="true"/ >
<p:selectBooleanCheckbox value="#{addressBean.copyBillingAddress}" id="duplicateBillingDetails">
<f:ajax render="@form" />
</p:selectBooleanCheckbox>
<h:panelGrid rendered="#{not addressBean.copyBillingAddress}" columns="3">
<p:inputText value="#{addressBean.deliveryAddress.firstName}"/>
</h:panelGrid>
<p:commandButton value="Checkout" action="#{addressBean.saveAddress}"/>
</h:form>
支持豆
@Component
@Scope("view")
public class AddressBean implements Serializable {
@Inject
private CurrentUserBean currentUserBean;
@Inject
private UserService userService;
private Address deliveryAddress = new Address();
private Address billingAddress = new Address();
private boolean copyBillingAddress;
public AddressBean() {
}
public boolean isCopyBillingAddress() {
return copyBillingAddress;
}
public void setCopyBillingAddress(boolean copyBillingAddress) {
this.copyBillingAddress = copyBillingAddress;
}
public String saveAddress() {
if (copyBillingAddress) {
deliveryAddress = new Address(billingAddress);
}
User user = currentUserBean.getUser();
if (!billingAddress.isSame(user.getBillingAddress())) {
user.setBillingAddress(billingAddress);
}
if (!deliveryAddress.isSame(user.getDeliveryAddress())) {
user.setDeliveryAddress(deliveryAddress);
}
currentUserBean.setUser(userService.save(user));
return "/checkout.xhtml";
}
public CurrentUserBean getCurrentUserBean() {
return currentUserBean;
}
public void setCurrentUserBean(CurrentUserBean currentUserBean) {
this.currentUserBean = currentUserBean;
}
public Address getDeliveryAddress() {
return deliveryAddress;
}
public void setDeliveryAddress(Address deliveryAddress) {
this.deliveryAddress = deliveryAddress;
}
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
}