Making Managed Beans Aware of the JSF Lifecycle
June 6, 2006 12 Comments
An event that runs when a JSF page is being loaded should be useful for initialization stuff. In my project team several developers use bean constructors to do such things which I think is a bad practice. Although I prefer the getters to do initializations, I’ve added a onPageLoad event to our application for people who prefer the constructors. It is similiar to Shale view mapper, the managed bean and the page name must be mapped in order to run the event. There are two things needed to do the job, a phaselistener and an interface to mark the managed beans. PhaseListener runs maps the viewId to the managed bean and executes the onPageLoad event.
And the interface that makes the managed bean aware of the lifecycle.
For example there is a page called index.jsp, and it is referred as the virtual mapping /index.jsf. Phaselistener extracts the index token and gives the mapping pc_Index. Also the managed bean class Index.java implements ILifeCycleAware interface and overrides the onPageLoad event. Then phaselistener executes this event. Finally the backing bean of index.jsp is declared in faces-config.xml as;
The good thing is more events can be added like the ones in Shale, eg prerender, preprocess and etc. So you can execute similiar events and make the managed beans aware of the lifecycle.