【问题标题】:What is the JSF behaviour, if you bind the same backing bean property to two input fields in the same form?如果将相同的支持 bean 属性绑定到相同表单中的两个输入字段,那么 JSF 行为是什么?
【发布时间】:2009-06-24 11:14:50
【问题描述】:

如果两个输入字段绑定到同一个会话范围的 Backing Bean 属性,JSF 中是否有定义的行为。

这是我的代码 sn-p

<h:form id="myForm">
   <h:inputText id="field1" value="#{TheBackingBean.theProperty}" />
   <h:inputText id="field2" value="#{TheBackingBean.theProperty}" />

   <h:commandButton id="continueButton" action="#{TheBackingBean.doSomething}" />
</h:form>

我的问题:如果 field1 和 field2 接收到不同的值,什么将绑定到支持 bean 属性?这甚至允许吗?

我知道这是一个粗略的场景。我的动机是,我们为我们的应用程序运行了 htmlunit 测试。在我们的 JSF 应用程序中,我们希望使用一个很酷的 ajaxified 自定义组件。这与 htmlunit 不能很好地协同工作。所以我的想法是,我只是放入一个绑定到相同属性的隐藏字段。然后单元测试填充隐藏字段而不是“真实”的东西。

问候

【问题讨论】:

    标签: jsf


    【解决方案1】:

    我认为这种代码是允许的,但是我不确定提交后theProperty的值。我认为 JSF 会做以下事情:

    TheBackingBean.setTheProperty(field1.value);
    TheBackingBean.setTheProperty(field2.value);
    

    但是,据我所知,没有任何内容指定 setter 调用的顺序。因此,在 update values JSF 阶段之后,您将无法确定 theProperty 是否等于 field1.valuefield2。价值

    关于您的场景,您说要将相同的属性绑定到 inputText 和 hiddenText。由于 hiddenText 不会提交其值,与 inputText 不同,不会出现此问题。事实上,如果你有这种 JSF 代码:

    <h:inputText id="field1" value="#{TheBackingBean.theProperty}"/>
    <h:inputHidden id="field2" value="#{TheBackingBean.theProperty}"/>
    

    那么 JSF 只会这样做:

    TheBackingBean.setTheProperty(field1.value);
    

    在提交阶段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-07
      • 2022-01-11
      相关资源
      最近更新 更多