4

I have an inputText:

<h:inputText id="result" value="#{guessNumber.result}"/>

and another inputText:

<h:inputText id="name" value="#{guessNumber.named}" onchange="submit()" valueChangeListener="#{guessNumber.processValueChange}"/>

and inside the processValueChange method, I added the following line:

result = "hello";

but the displayed value of "result" inputText remains unchainged, what is the problem?

O. Salah
  • 189
  • 1
  • 2
  • 13

2 Answers2

12

The valueChangeListener method isn't intended to make changes to the model this way. It's intented to listen on the actual change of the model value, at exactly the moment where you have a hand of both the old and new model value. For example, to perform some logging. It runs at the end of the validations phase, right before the update model values phase. So any changes which you make to the model values yourself inside the listener method would be overridden during the update model values phase.

You need a <f:ajax listener> instead. This runs during the invoke action phase, which is after the update model values phase.

<h:outputText id="result" value="#{guessNumber.result}" />
<h:inputText id="name" value="#{guessNumber.named}">
    <f:ajax listener="#{guessNumber.namedChanged}" render="result" />
</h:inputText>

(note that I've removed the onchange="submit()" JavaScript handler!)

with

public void namedChanged(AjaxBehaviorEvent event) {
    result = "Hello, you entered " + named;
}

(the argument is optional; if you don't need it, just omit it)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hello BalusC, I tried your suggestion but still not working, nothing happens when "name" inputText loses focus, the only line included inside namedChanged method is: result = "hello"; – O. Salah Sep 28 '12 at 19:14
  • This should have been the accepted answer. Binding is kind of tricky and might get you into trouble. – Jasper de Vries May 22 '17 at 08:35
3

I found the solution: Instead of binding the value attribute of the inputText to a String property (result) inside the bean, I bound the inputText to UIInput property inside the bean:

<h:inputText id="result" binding="#{guessNumber.result}"/>

and in the bean:

private UIInput result = null;

this lets the listener method modify the inputText value directly, instead of modifying the property that is bound to the value attribute of the inputText:

result.setValue("Hello");

Thanks for every one tried to solve my problem.

O. Salah
  • 189
  • 1
  • 2
  • 13