为什么p:resetInput要求在提交表单后首先将托管bean的属性设置为null?

Tin*_*iny 3 jsf primefaces jsf-2

在视图范围内的托管bean中,我<p:resetInput>用来清除相应的manged bean中属性所持有的值,比如

<p:commandButton value="Reset" update="panel" process="@this">
    <p:resetInput target="panel" />
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)

这很好用.


我有一个提交按钮<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>
Run Code Online (Sandbox Code Playgroud)

命令按钮调用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.
}
Run Code Online (Sandbox Code Playgroud)

如果reset()省略此方法,则<p:inputText>不会被清除,但如果我按下XHTML中所示的重置按钮,则<p:inputText>应该清除但不会.

展示例子说明了同样的事情.因此,这种行为似乎有文件记载但我不明白为什么不<p:resetInut>清除值property1,如果reset()方法被省略,在这种情况下?

Bal*_*usC 8

<p:resetInput>你错误地似乎都期望不清除的模型值.它只是清除输入组件的状态,在验证错误后它可能是脏的.

它试图解决的具体问题在这个答案中详细描述:如何在验证错误发生后使用PrimeFaces AJAX填充文本字段?

以下用例最能理解这一点:

  1. 您有一个带有单个数据表的视图和一个显示当前所选记录以进行编辑的对话框.
  2. 您打开对话框并使用无效值提交其表单.输入组件标记为无效并以红色突出显示.
  3. 您关闭对话框而不修复错误.
  4. 然后选择相同或另一行进行编辑.对话框显示,但输入组件仍然标记为无效并突出显示为红色并显示旧的提交值-if any-因为它仍然是您正在使用的相同视图状态.

<p:resetInput>在"打开对话框"按钮,对话框的形式与目标修复它.

我不确定您的特定案例是否<p:resetInput>是正确的解决方案.您的代码不完整,并且您没有在任何地方说明此代码背后的具体功能要求,但据我所知,没有多个输入/表单需要相互更新.我相信即使你删除你的案子仍然有效<p:resetInput>.因此,在您的上下文中它将是完全超级的,您可以放弃清除模型(或者只是通过同步GET按钮刷新页面,隐式重新创建视图).

也可以看看: