我在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>
支持Bean
@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;
}
}
答案 0 :(得分:4)
默认情况下,<f:ajax>
进程仅与execute="@this"
中的当前组件相同。因此,不会处理所有其他输入组件的提交值,因此不会更新到模型中。但是,您可以通过render="@form"
强制使用所有其他输入组件的当前(未更新!)模型值刷新表单的整个HTML输出。
假设您的意图是不不必要地转换/验证/更新所有其他输入组件,您最好使render
属性更具体。仅更新真正需要更新的内容,而不是整个表单。
<p:selectBooleanCheckbox value="#{addressBean.copyBillingAddress}" id="duplicateBillingDetails">
<f:ajax render="copyBillingAddress" />
</p:selectBooleanCheckbox>
<h:panelGroup id="copyBillingAddress">
<h:panelGrid rendered="#{not addressBean.copyBillingAddress}" columns="3">
<p:inputText value="#{addressBean.deliveryAddress.firstName}"/>
</h:panelGrid>
</h:panelGroup>
或者,如果您确实需要render="@form"
,则需要添加execute="@form"
以告知<f:ajax>
同时处理所有其他输入组件。
<f:ajax execute="@form" render="@form" />
请注意,这可能会不必要地触发这些输入组件的转换/验证。