Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

jsf 2 - Change inputText value from listener method

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...