Indeed just use the bean's constructor or initialization block. You can also set it as a managed property in the faces-config.xml. Since JSF 1.2 you can also create a method with the @PostConstruct annotation, that method will be fired when the bean is created and all of its managed properties are been set. In JSF you can also make use of the 'beforePhase' and 'afterPhase' attributes of the f:view tag which you bind to a method which takes a PhaseEvent argument. In there you can check the current PhaseId and handle accordingly.
Originally posted by Himanshu Gupta:
why don't you do it in constructor of the bean itself. Anyways whenever a page is created its corresponding bean is also created.
Only if the bean is request scoped -which is the right design approach indeed.
Originally posted by venkatesh mr:
But if the RI you are using is not good enough, the getter methods may be invoked during the validation or conversion phases too.. So, it may be useful to see which phase you are in by checking the phase id before executing the code..
So you're saying that this is due to the RI? You're wrong. And you're also wrong to suggest to place this kind of logic in the getters. Getters are solely there to
return data. You however can consider introducing lazy loading in getters, e.g. if (data == null) { loadData(); } return data; so that it won't be executed more than once in bean's life.
[ September 20, 2008: Message edited by: Bauke Scholtz ]