tags:

views:

475

answers:

1

Hello,

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

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?

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.

Regards

+2  A: 

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:

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.

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:

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

then JSF will only do:

TheBackingBean.setTheProperty(field1.value);

during the submission phase.

romaintaz