Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change inputText value from listener method

Tags:

jsf-2

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?

like image 558
O. Salah Avatar asked Dec 12 '22 22:12

O. Salah


1 Answers

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:

  • When to use valueChangeListener or f:ajax listener?
like image 156
BalusC Avatar answered Feb 22 '23 18:02

BalusC