如果将相同的backing bean属性绑定到同一表单中的两个输入字段,那么JSF的行为是什么?

时间:2022-08-25 05:02:13

Is there a defined behaviour in JSF, if two input fields are bound to the same session scoped Backing Bean property.

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

Here is my code snippet

这是我的代码片段

<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>

My question: If field1 and field2 receive different values, what will be bound to the backing bean property? Is this even allowed?

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

I know this is a crude scenario. My motivation is, that we have htmlunit tests running for our application. In our JSF application we want to use a cool ajaxified custom component. This doesnt work together very well with htmlunit. So my idea was, I just put in a hidden field that binds to the same property. The unit test then fills the hidden field instead of the "real" thing.

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

Regards

1 个解决方案

#1


I think this kind of code is allowed, but I am not sure of the value of theProperty after the submission. What I think is that JSF will do the following:

我认为这种代码是允许的,但我不确定提交后属性的价值。我认为JSF将执行以下操作:

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

However, nothing - as far as I know - specifies the order of the setter calls. Thus, after the update values JSF phase, you will not be sure if theProperty will be equal to field1.value or field2.value.

但是,据我所知,没有任何内容指定了setter调用的顺序。因此,在更新值JSF阶段之后,您将无法确定属性是否等于field1.value或field2.value。

Concerning your scenario, you say that you want to bind the same property to an inputText and an hiddenText. As the hiddenText will not submit its value, unlike the inputText, this problem will not occur. Indeed, if you have this kind of JSF code:

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

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

then JSF will only do:

然后JSF只会这样做:

TheBackingBean.setTheProperty(field1.value);

during the submission phase.

在提交阶段。

#1


I think this kind of code is allowed, but I am not sure of the value of theProperty after the submission. What I think is that JSF will do the following:

我认为这种代码是允许的,但我不确定提交后属性的价值。我认为JSF将执行以下操作:

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

However, nothing - as far as I know - specifies the order of the setter calls. Thus, after the update values JSF phase, you will not be sure if theProperty will be equal to field1.value or field2.value.

但是,据我所知,没有任何内容指定了setter调用的顺序。因此,在更新值JSF阶段之后,您将无法确定属性是否等于field1.value或field2.value。

Concerning your scenario, you say that you want to bind the same property to an inputText and an hiddenText. As the hiddenText will not submit its value, unlike the inputText, this problem will not occur. Indeed, if you have this kind of JSF code:

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

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

then JSF will only do:

然后JSF只会这样做:

TheBackingBean.setTheProperty(field1.value);

during the submission phase.

在提交阶段。