为什么在 ajax 使用 Primefaces 更新后字段会重置
Why are fields reset after ajax update with Primefaces
在一个表单上,我有一些个人信息(姓名、地址等)和一个根据性别和姓氏更新称呼和字母称呼的 changeListener。如果底层实体已经存储在数据库中,它就可以正常工作。如果我正在为新的(未保存的)条目输入数据,则在调用侦听器后会重置表单。输入的所有数据都丢失了,只保留了我在听众中修改的称呼、字母称呼和我使用并附加了 ajax 调用的性别、姓名。
这是表格的一部分:
<h:form id="person">
<p:panel header="#{msg['prs']}">
<h:panelGrid columns="6" cellpadding="4">
<h:outputText value="#{msg['prs.salutation']}"/>
<p:inputText value="#{personBean.selectedPerson.salutation}"/>
<h:outputText value="#{msg['prs.lettersalutation']}"/>
<p:inputText value="#{personBean.selectedPerson.letterSalutation}"/>
<p:spacer/><p:spacer/>
<h:outputText value="#{msg['prs.name']}: "/>
<p:inputText value="#{personBean.selectedPerson.name}">
<p:ajax event="change" update="person"
listener="#{personBean.selectedPerson.updateSalutation}" />
</p:inputText>
<h:outputText value="#{msg['prs.surname']}: "/>
<p:inputText value="#{personBean.selectedPerson.surname}"/>
<h:outputText value="#{msg['prs.gender']}: "/>
<p:selectOneMenu value="#{personBean.selectedPerson.gender}">
<f:selectItems value="#{enumHelper.getEnum('Gender')}"/>
<p:ajax event="change" update="person"
listener="#{personBean.selectedPerson.updateSalutation}" />
</p:selectOneMenu>
</p:panel>
</h:form>
我在代码中进行更新。
public void updateSalutation() {
// simplified
letterSalutation = "...";
salutation = "...";
// outputs for debug
System.out.println(this.getName()); // --> not null
System.out.println(this.getSurname()); // --> null
}
此调用中的姓氏(未附加到 ajax 调用中已经为空,即使数据已在此处输入。所有其他领域也是如此。在我的应用程序中,我使用 Primefaces、JavaEE 1.6 和 Wildfly。
这种行为的原因是什么?我可以更改通话内容以防止出现这种情况吗?
Why are fields reset after ajax update with Primefaces
根据具体的功能需求,这个问题有两种可能的原因:
这是因为您没有告诉 JSF 处理所需的输入值。
或者,这是因为您告诉 JSF 也更新不需要的输入值。
解决方法取决于原因:
将process="person"
(或process="@form"
)添加到<p:ajax>
以处理整个表格。
或者,编辑update="person"
以指定仅真正需要更新的组件。
鉴于您在问题末尾的抱怨,我相信您正在寻找#1。
另请参阅:
- Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
我的解决方案:
在
中包含我要更新的内容
<p:panel id="toUpdate">
并制作一个
update="toUpdate"
在一个表单上,我有一些个人信息(姓名、地址等)和一个根据性别和姓氏更新称呼和字母称呼的 changeListener。如果底层实体已经存储在数据库中,它就可以正常工作。如果我正在为新的(未保存的)条目输入数据,则在调用侦听器后会重置表单。输入的所有数据都丢失了,只保留了我在听众中修改的称呼、字母称呼和我使用并附加了 ajax 调用的性别、姓名。
这是表格的一部分:
<h:form id="person">
<p:panel header="#{msg['prs']}">
<h:panelGrid columns="6" cellpadding="4">
<h:outputText value="#{msg['prs.salutation']}"/>
<p:inputText value="#{personBean.selectedPerson.salutation}"/>
<h:outputText value="#{msg['prs.lettersalutation']}"/>
<p:inputText value="#{personBean.selectedPerson.letterSalutation}"/>
<p:spacer/><p:spacer/>
<h:outputText value="#{msg['prs.name']}: "/>
<p:inputText value="#{personBean.selectedPerson.name}">
<p:ajax event="change" update="person"
listener="#{personBean.selectedPerson.updateSalutation}" />
</p:inputText>
<h:outputText value="#{msg['prs.surname']}: "/>
<p:inputText value="#{personBean.selectedPerson.surname}"/>
<h:outputText value="#{msg['prs.gender']}: "/>
<p:selectOneMenu value="#{personBean.selectedPerson.gender}">
<f:selectItems value="#{enumHelper.getEnum('Gender')}"/>
<p:ajax event="change" update="person"
listener="#{personBean.selectedPerson.updateSalutation}" />
</p:selectOneMenu>
</p:panel>
</h:form>
我在代码中进行更新。
public void updateSalutation() {
// simplified
letterSalutation = "...";
salutation = "...";
// outputs for debug
System.out.println(this.getName()); // --> not null
System.out.println(this.getSurname()); // --> null
}
此调用中的姓氏(未附加到 ajax 调用中已经为空,即使数据已在此处输入。所有其他领域也是如此。在我的应用程序中,我使用 Primefaces、JavaEE 1.6 和 Wildfly。
这种行为的原因是什么?我可以更改通话内容以防止出现这种情况吗?
Why are fields reset after ajax update with Primefaces
根据具体的功能需求,这个问题有两种可能的原因:
这是因为您没有告诉 JSF 处理所需的输入值。
或者,这是因为您告诉 JSF 也更新不需要的输入值。
解决方法取决于原因:
将
process="person"
(或process="@form"
)添加到<p:ajax>
以处理整个表格。或者,编辑
update="person"
以指定仅真正需要更新的组件。
鉴于您在问题末尾的抱怨,我相信您正在寻找#1。
另请参阅:
- Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
我的解决方案: 在
中包含我要更新的内容<p:panel id="toUpdate">
并制作一个
update="toUpdate"