具有即时="true"的JSF commandButton; [英] JSF commandButton with immediate="true"

查看:98
本文介绍了具有即时="true"的JSF commandButton;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,有一个selectOneMenu的值绑定到支持bean.

I have a situation where there is a selectOneMenu that has a value bound to a backing bean.

我需要有一个不会更新模型值的按钮(这就是为什么它具有即时的"true"属性).

I need to have a button that doesn't update model values (that is why it has immediate="true" property).

该按钮的操作方法更改了selectOneMenu绑定的值,但是当重新显示页面时,将显示原始值(已提交的值),而不是操作方法中设置的值.

That button's action method changes the value the selectOneMenu is bound to, but when the page is redisplayed the original value is displayed (the one that was submitted) and not the one set in the action method.

为什么会发生这种情况?

Any ideas why that is happening?

如果我对问题的解释不够好,请告诉我.

If I didn't explain the problem good enough please let me know.


根据要求,这里是有问题的源代码:


As requested here is the source code in question:

<h:selectOneMenu id="selectedPerson" 
                 binding="#{bindings.selectPersonComponent}" 
                 value="#{bean.selectedPerson}">
   <s:selectItems var="op" value="#{bean.allPersons}" 
                  label="#{op.osoba.ime} #{op.osoba.prezime}" 
                  noSelectionLabel="#{messages.selectAPerson}">
   </s:selectItems>
   <f:converter converterId="unmanagedEntityConverter" />
</h:selectOneMenu>
...
<a4j:commandButton action="#{bean.createNew}" value="#{messages.createNew}"
     immediate="true" reRender="panelImovine">
</a4j:commandButton>

java代码:

private Person selectedPerson;

public String createNew() {
    log.debug("New created...");
    selectedPerson = null;
    bindings.getSelectPersonComponent().setSubmittedValue(null); //SOLUTION
    return "";
}

解决方案位于标记为 SOLUTION :)

The solution is in the lined marked SOLUTION :)

推荐答案

由于在发布此问题后不久经常发生,所以我找到了答案:

As it frequently happens a few moments after posting this question I have found an answer:

在此处详细说明了问题的原因: ClearInputComponents

The cause of the problem is in detail explained here: ClearInputComponents

问题是(如所解释的)模型值尚未更新,因此提交的输入仍在component.submittedValue字段中,如果不为空,则显示该字段.更新模型后,通常可以将其清空.

The problem is (as explained) that model values haven't been updated so the submitted inputs are still in component.submittedValue field and that field is displayed if not empty. It is emptied normally after model has been updated.

在我的情况下,第一个解决方案不起作用,因为视图中还有其他重要状态也不能迷失.但是第二种解决方案效果很好:

The first solution didn't work in my case because there is other important state in the view that mustn't get lost. But the second solution worked great:

component.setSubmittedValue(null);

这就是所有需要的事情:这是一件额外的工作,因为组件必须绑定到某个bean,但是还不错.

And that was all that was needed: it is a little extra work because components must be bound to some bean, but not that bad.

这篇关于具有即时="true"的JSF commandButton;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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