如何将JSF UIInput组件重置为其托管bean值 [英] How can I reset JSF UIInput components to their managed bean values

查看:153
本文介绍了如何将JSF UIInput组件重置为其托管bean值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在验证失败后将JSF输入重置为原始托管bean值。

I want to reset JSF inputs to their original managed bean values after validation failed.

我在同一页面中有两个表单 - 第一个表单有一个 commandLink 初始化第二个表单。第二种形式呈现为一个对话框,其可见性通过jQuery切换 - 为了本练习的目的,我可以在同一页面上用两个表单来说明。此外,当我在我的应用程序中使用PrimeFaces 2.2.x时,同样的行为也出现在常规 h:commandLink 中。

I have two forms inside the same page - the first form has a commandLink to initialize the second form. The second form is rendered as a dialog whose visibility is toggled through jQuery - for the purpose of this exercise, though, I can illustrate just with two forms on the same page. Also, while I'm using PrimeFaces 2.2.x in my app, the same behaviors appear with regular h:commandLink as well.

我遇到的问题是:


  1. 点击第一张表格中的链接初始化第二个表单

  2. 以第二种形式提交无效值

  3. 再次单击第一个表单中的链接以初始化第二个表单 - 仍然存在无效值和/或UIInput状态仍然无效。

  1. click link in first form to initialize second form
  2. submit invalid values in second form
  3. click link in first form again to initialize second form - invalid values still there and/or UIInput state is still invalid.

例如 - 采取以下表格

For example - take the following form

<h:form id="pageForm">
    <h:commandLink actionListener="#{testBean.initialize}">Initialize, no execute
        <f:ajax render=":dialogForm"/>
    </h:commandLink>
    <br/>
    <h:commandLink actionListener="#{testBean.initialize}">Initialize, execute=@this
        <f:ajax execute="@this" render=":dialogForm"/>
    </h:commandLink>

</h:form>

<h:form id="dialogForm">
    <h:messages/>
    String property - Valid: <h:outputText value="#{property.valid}"/>
    <br/>
    <h:inputText id="property" binding="#{property}" value="#{testBean.property}">
    <f:validateLength minimum="3"/>                         
    </h:inputText>
    <br />
    Int property - Valid: <h:outputText value="#{intValue.valid}"/>
    <h:inputText id="intValue" binding="#{intValue}" value="#{testBean.intValue}">
        <f:validateLongRange maximum="50" />                            
    </h:inputText>
    <br/>

    <h:commandLink actionListener="#{testBean.submit}">
        Submit
        <f:ajax render="@form" execute="@form"/>
    </h:commandLink>

    <h:commandLink actionListener="#{testBean.initialize}">Initialize, execute=@this
        <f:ajax execute="@this" render="@form"/>
    </h:commandLink>

</h:form>

Bean类:

@ManagedBean
@ViewScoped
public class TestBean {
  private String property = "init";
  private Integer intValue = 33;
  // plus getters/setters

  public void submit() { ... }
  public void initialize() {
   intValue = 33;
   property = "init";   
  }

}

行为#1


  1. 点击pageForm上的Initialize链接

  2. 输入初始化为 init,33

  3. 现在为两个字段提交无效内容,例如aa,99

  4. 现在点击任何初始化 再次链接(它们似乎都表现相同 - 无论是相同形式还是不同,或者我是否指定了执行=@ this,都没有区别。)

  1. click either "Initialize" link on the pageForm
  2. inputs get initialized to "init", "33"
  3. now submit something invalid for both fields like "aa", "99"
  4. now click any of the "initialize" links again (they all seem to behave the same - makes no difference whether it's in the same form or different, or whether I have specified execute="@this" or not.)

结果=> UIInput.isValid() = false,两个值都重置(init,33)。

Result => UIInput.isValid() = false, both values reset though ("init", "33").

Expected => valid = true(或者这是不合理的?)

Expected => valid = true (or is this not reasonable to expect?)

行为#2


  1. 点击pageForm上的初始化链接

  2. 输入获取初始化为init,33

  3. 现在提交文本字段无效但对int字段有效(aa,44)

  4. 现在再次单击任何初始化链接
  1. click either "Initialize" link on the pageForm
  2. inputs get initialized to "init", "33"
  3. now submit something invalid for the text field but valid for the int field ("aa", "44")
  4. now click any of the "initialize" links again

Result =>init,valid = false; 44,valid = true

Result => "init", valid=false; 44, valid=true

Expected =>init,valid = true; 33,有效=真

Expected => "init", valid=true; 33, valid=true

我也看过:

JSF 2 - Bean验证:验证失败 - >空值将替换为来自托管bean的上一个有效值

如何使用填充文本字段?发生验证错误后PrimeFaces AJAX?

使用<显式重置 UIInputs 状态的建议code> resetValue 工作,但我对它不满意。

The suggestion to explicitly reset the state of UIInputs with resetValue does work, but I'm not happy with it.

现在,我有点理解为什么isValid没有重置 - 我对JSF生命周期的理解是,一旦将值提交给组件,isValid就不会被重置,直到成功提交和验证组件并且Update Model Values阶段设置bean值。因此,在这种情况下,可能无法明确重置有效状态,因为我想使用#{foo.valid} 进行条件CSS样式。

Now, I sort of understand why the isValid is not resetting - my understanding of the JSF lifecycle is that once a value is submitted to a component, isValid is not reset until the component is successfully submitted and validated and the Update Model Values phase sets the bean value. So there may be no way around explicitly resetting the valid state in this case, since I want to use #{foo.valid} for conditional CSS styling.

但是,我不明白为什么成功验证的组件不会从bean重新初始化。也许我对JSF生命周期的理解有点偏差?

What I don't understand, though, is why the components that successfully validated are not re-initializing from the bean. Perhaps my understanding of the JSF lifecycle is slightly off?

我理解如何在发生验证错误后使用PrimeFaces AJAX填充文本字段?因为它们属于单个组件但不属于作为一个整体的形式 - 即,如果一个组件成功验证但验证整体失败会发生什么?

I understand the rules layed out in the answer to How can I populate a text field using PrimeFaces AJAX after validation errors occur? as they pertain to an individual component but not to the form as a whole - i.e., what happens if a component succeeds validation but the validation overall fails?

推荐答案

事实上,可能没有比明确调用 resetValue 关于组件。在我的例子中,所有对话框都在同一个大JSF视图树中,底层页面打开它们。因此,从JSF的角度来看,应该保留包含无效输入值的相同视图组件状态,直到我们离开视图为止,因为它无法看到我们如何在客户端切换显示属性。

In fact, there may turn out to be no better way than explicitly calling resetValue on components. In my case, all of the dialogs are in the same big JSF view tree with the underlying page that opens them. So from JSF's perspective, the same view component state including invalid input values should be preserved until we navigate away from the view, as it has no visibility into how we're toggling display attributes client-side.

可能工作的唯一另一件事是,构成对话框的组件实际上没有在JSF视图树中呈现,除非它们可见。就我而言,它们总是被渲染,使用CSS来切换可见性。

The only other thing that might work is if the components that make up the dialog are actually not rendered in the JSF view tree unless they're visible. In my case, they're always rendered, using CSS to toggle visibility.

这篇关于如何将JSF UIInput组件重置为其托管bean值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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