我想到了一个奇怪的问题。我试图隔离问题,因此以下是我的简化代码。
public class MyBean {
private List<Data> dataList;
Data selectedData;
public MyBean() {
dataList = new ArrayList<Data>();
dataList.add(new Data("John", 16));
dataList.add(new Data("William", 25));
}
public List<Data> getDataList() {
return dataList;
}
public void edit(Data data) {
selectedData = data;
}
public void newData() {
selectedData = new Data(null, null);
}
public Data getSelectedData() {
return selectedData;
}
public class Data {
String name;
Integer age;
Data(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
xhtml:
<rich:modalPanel id="pop">
<h:form>
Name: <h:inputText value="#{myBean.selectedData.name}" required="true" id="txtName"/><br/>
Age : <h:inputText value="#{myBean.selectedData.age}" required="true" id="txtAge"/>
<a4j:commandButton value="Save"/>
<a4j:commandButton value="Close" onclick="Richfaces.hideModalPanel('pop');return false;"/>
<br/>
<rich:message for="txtName"/><br/>
<rich:message for="txtAge"/>
</h:form>
</rich:modalPanel>
<h:form>
<rich:dataTable value="#{myBean.dataList}" var="data">
<rich:column>#{data.name}</rich:column>
<rich:column>
<a4j:commandLink value="Edit" action="#{myBean.edit(data)}" reRender="pop" oncomplete="Richfaces.showModalPanel('pop')"/>
</rich:column>
</rich:dataTable>
<a4j:commandButton value="New" action="#{myBean.newData()}" reRender="pop" oncomplete="Richfaces.showModalPanel('pop')"/>
</h:form>
这是错误的路径:
- 加载页面
- 单击第一行中的“编辑”链接(弹出窗口显示)
- 在弹出窗口中,清除“年龄”字段,然后单击“保存”。(显示必填消息)
- 点击取消(不填写“年龄”字段)
- 单击第二个链接。
- 现在它显示不相关的数据(以前的数据)。 - 这就是问题
- 即使我单击“新建”按钮,它也会显示不正确的数据。
仅当弹出窗口中验证失败时才会发生这种情况。
有解决办法吗?
这个问题在JSF 2中也被认可并在以下答案中详细解释:发生验证错误后如何使用 PrimeFaces AJAX 填充文本字段? https://stackoverflow.com/questions/6642242/how-can-i-populate-a-text-field-using-primefaces-ajax-after-validation-errors-oc/6845800#6845800如果您使用 JSF 2,您可以使用 OmniFaces'ResetInputAjaxActionListener http://showcase.omnifaces.org/eventlisteners/ResetInputAjaxActionListener或 PrimeFaces'<p:resetInput> or resetValues="true" https://www.primefaces.org/showcase/ui/misc/resetInput.xhtml为了这。
到目前为止,您需要清除该状态EditableValueHolder http://docs.oracle.com/javaee/5/api/javax/faces/component/EditableValueHolder.html组件即将被 ajax 渲染,但不包含在 ajax-execute 中。要清除状态,在 JSF 2 中您可以使用便捷方法resetValue() http://docs.oracle.com/javaee/6/api/javax/faces/component/EditableValueHolder.html#resetValue%28%29为此,但这在 JSF 1.2 中不可用,您需要调用 4 个单独的方法setValue(null)
, setSubmittedValue(null)
, setLocalValueSet(false)
, setValid(true)
清除状态。
要确定哪些组件将被 ajax 渲染,但没有被 ajax 执行,在 JSF 2 中您可以使用PartialViewContext
方法,但这在 JSF 1.2 中不可用,因为 JSF 1.2 尚未标准化 ajax。您需要摆弄 RichFaces 特定的 ajax API 才能弄清楚这一点。我无法从头顶看出这一点,所以这里有一个启动示例,假设您已经知道需要清除的组件。想象一下弹出窗口中的表单有id="popForm"
名称输入字段有id="nameInput"
,这是您如何在内部清除它的方法newData()
method:
UIInput nameInput = (UIInput) context.getViewRoot().findComponent("popForm:nameInput");
nameInput.setValue(null);
nameInput.setSubmittedValue(null);
nameInput.setLocalValueSet(false);
nameInput.setValid(true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)