JSF即时=“真"取消按钮不起作用 [英] JSF immediate="true" for cancel button doesn't work

查看:117
本文介绍了JSF即时=“真"取消按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选项卡页面,一个选项卡是记录列表,单击记录将切换到编辑"选项卡,编辑"选项卡中有保存"和取消"按钮.

I have a two tab page, one tab is the record list, click on the record will switch to the Edit tab, and there's Save and Cancel buttons in the Edit tab.

现在,我单击记录#1,进行一些编辑,然后单击取消"按钮.当然,我不想验证表单,因为它已被取消,因此我在取消"按钮上设置了immediate="true".现在,编辑选项卡已关闭,返回到记录列表.然后,我单击另一个记录#2,出现了问题:在编辑"选项卡中,它仍然是记录#1的先前内容,而不是记录#2.我已经在调试视图中检查了变量,编辑表单的back bean实际上已经填充了记录#2.

Now, I click on the record #1, do some edit, and click the Cancel button. Certainly I don't want to validate the form because it's canceled, so I set immediate="true" on the Cancel button. Now the edit tab is closed, back to the record list. Then, I click on another record #2, the problem occurred: In the edit tab, it's still the previous content of record #1, rather then record #2. I've checked the variables in debug view, the back bean for the edit form was ACTUALLY filled with record #2.

即,在立即执行命令后出现了一些问题.

I.e., something broken after an immediate command.

(在我添加验证和即时="true"之前,一切都很好.)

(Everything had been well before I have added the validation and immediate="true".)

class FormBean {

    Record activeRecord;
    ...

    public void clickOnList() {
        activeRecord = loadRecord(clickIndex);
    }

    public void cancelForm() {
        activeRecord = null;
    }

}

page.xhtml:

page.xhtml:

<h:form id="main">
  ...
  <p:tab title="Edit" rendered="#{formBean.activeRecord != null}">
    ...
    <p:commandButton value="Cancel"
        actionListener="#{formBean.cancelForm}"
        update="main" async="true" immediate="true" />
  </p:tab>
</h:form>

推荐答案

immediate="true"与同一视图上的ajax请求不能很好地配合使用.归结为输入组件中仍存在未提交但尚未验证的值.如果没有ajax,则后续表单提交的常规同步请求/响应将无提示地解决"此问题.但是使用ajax不会发生这种情况.先前的ajax请求的提交值仍在输入组件中.当 UIInput#getSubmittedValue() 不返回时null,无论模型值(已更改)如何,都将显示它.

The immediate="true" doesn't work nicely with ajax requests on the same view. It boils down to that the submitted-but-not-validated values are still there in the input components. Without ajax, a normal synchronous request/response of a subsequent form submit would silently "solve" this. But with ajax, this does not happen. The submitted values of the previous ajax request are still there in the input components. When UIInput#getSubmittedValue() doesn't return null, it will be displayed instead, irrespective of the (changed) model value.

基本上,当您想使用ajax作为取消按钮时,您需要排除要处理的输入,而不是依靠immediate="true"(这实际上是一种技巧) .用标准的JSF <f:ajax>术语,您可以使用按钮上的execute="@this"(实际上是默认值)而不是execute="@form"来执行此操作. PrimeFaces按钮默认为@form,因此您需要更改此设置:

Basically, when you want to stick to ajax for a cancel button, you need to exclude the inputs from being processed instead of relying on the immediate="true" (which is actually kind of a hack). In standard JSF <f:ajax> terms, you could do this with execute="@this" (which is actually the default) on the button instead of execute="@form". The PrimeFaces buttons defaults to @form, so you need to change this:

<p:commandButton value="Cancel"
    actionListener="#{formBean.cancelForm}"
    update="main" async="true" process="@this" />

这篇关于JSF即时=“真"取消按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆