在 View 范围的托管 bean 中,我使用 <p:resetInput>
清除相应管理 bean 中的属性所持有的值,例如,
<p:commandButton value="Reset" update="panel" process="@this">
<p:resetInput target="panel" />
</p:commandButton>
这很好用。
我有一个提交按钮 <p:commandButton>
如果验证成功,按下它会导致提交的值被插入到数据库中。
<p:remoteCommand name="updateTable" update="dataTable"/>
<p:panel id="panel" header="New">
<p:outputLabel for="property1" value="property1"/>
<p:inputText id="property1" value="#{bean.property1}" required="true">
<f:validateLength minimum="2" maximum="100"/>
</p:inputText>
<p:message for="property1" showSummary="false"/>
<p:commandButton id="btnSubmit"
update="panel messages"
oncomplete="if(!args.validationFailed) {updateTable();}"
actionListener="#{bean.insert}"
value="Save"/>
<p:commandButton value="Reset" update="panel" process="@this">
<p:resetInput target="panel" />
</p:commandButton>
</p:panel>
命令按钮调用 insert()
托管bean中的方法,定义如下。
public void insert() {
if (service.insert(property1)) {
//...Popup a success message.
reset(); //Invoke the following private method.
} else {
//...Show the cause of the failure.
}
}
private void reset() {
property1 = null; //Set this property of type String to null.
}
如果这个 reset()
方法省略,则为 <p:inputText>
不会被清除,但是如果我按下 XHTML 中所示的重置按钮,<p:inputText>
应该被清除,但它没有。
showcase example演示完全相同的事情。因此,这种行为似乎已记录在案,但我不明白为什么 <p:resetInut>
清除 property1
的值, 如果 reset()
方法被省略了,在这种情况下?
最佳答案
<p:resetInput>
不会像您错误地预期的那样清除模型值。它只是清除输入组件的状态,在验证错误后可能是脏的。
这个答案详细描述了它试图解决的具体问题:How can I populate a text field using PrimeFaces AJAX after validation errors occur?
下面的用例最好理解这一点:
- 您有一个 View ,其中包含一个数据表和一个显示当前选择的记录以供编辑的对话框。
- 您打开对话框并提交带有无效值的表单。输入组件被标记为无效并以红色突出显示。
- 您关闭对话框而不修复错误。
- 然后您选择同一行或另一行进行编辑。对话框出现了,但输入组件仍被标记为无效并以红色突出显示,并显示旧提交的值 - 如果有的话 - 因为它仍然是您正在使用的相同 View 状态。
推杆 <p:resetInput>
在“打开对话框”按钮中以对话框形式为目标进行修复。
我不确定您的特殊情况是否适合 <p:resetInput>
是正确的解决方案。您的代码不完整,并且您没有在任何地方说明此代码背后的具体功能要求,但据我所知,没有多个输入/表单需要相互更新。我相信即使您删除 <p:resetInput>
,您的案例仍然有效。 .因此,在您的上下文中这将是完全多余的,您可以直接清除模型(或者......只需通过隐式重新创建 View 的同步 GET 按钮刷新页面)。
另见:
关于jsf - 为什么 p :resetInput require properties of a managed bean to be set to null first after the form is submitted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17674734/