views:

513

answers:

1

Hi,

I have a jsf page with a form has an outputtext in it. The value of outputtext component is called from a backing bean (or managed bean). I know when I code it as #{MyBean.myString} Jsf rename it and calls getMyString() method. However the wierd thing is, when I put a breakpoint to the getter method of this component, I see it is called twice during the page is being rendered. The outputtext is in a h:form, and it is the only component wich is bind to a backingbean. I mean, it is so wierd that jsf should get the value when it first come to the getter method, however it needs to go to the getter method twice. Can you explain what is the reason of this behaviour in jsf?

Any help would be appreciated, Best wishes, Baris

+2  A: 

The getter, as its name already self-describes, is just there with the pure purpose to retrieve the data. JSF doesn't cache this data. It will call it whenever needed. The cost of calling a getter is in practice nihil --unless you do something more than returning the data, e.g. hitting the DB everytime, this logic should then be moved out of the getter or be turned into lazy loading.

In case of a form submit, the first get call is usually fired during validations phase to check if there is any initial value so that JSF can handle the value change event. The second call is usually fired during render response phase to display the model value in the view.

You may find this article useful as well to learn more about the JSF lifecycle. You may find this answer useful to learn more about ways to do preprocessing/initialization in a backing bean.

BalusC
Thank you so much. BTW I find it worthy to mention that I didnt mean form submit, yet I mean the first phase of the request, when the page is being rendered for the first time. During that process, the components value (the getter method) was being called twice, however I think when the first time reaches the getter method, it must get the value of the variable, doesnt it?I will read the articles you posted, and if you have any more idea about it I would be glad if you share with me.Thanks again,Baris
Bariscan
Then you've bound this property twice somewhere in the view. Add `Thread.dumpStack()` to the getter method to learn more about the call stack so that you can figure where it originated.
BalusC