Originally posted by Merrill Higginson:
I'm a relative newbie to JSF. I understand how to link actions with methods in backing beans, but there's one thing I don't understand: Where is the best place to put code that has to execute before a page is displayed?
For example, suppose a page has a select control with a list of options that must be populated before the page is displayed. I understand that I could store this List in the property of a session scoped bean and bind the bean property to the control. But where should I put the code to populate the list? Should it go in the constructor of the session-scoped bean? Is there a "page about to be rendered" event that I can bind a method to?
[ September 12, 2006: Message edited by: Merrill Higginson ]
Hi Merrill,
This one slipped through the cracks. Actually in your case you don't have to always execute a bit of code to prepare data for your page - like a
Struts action. You can, but you don't have to. Taking your scenario as an example, you can rely on the managed beans facility within JSF to prepare any and all your
Java classes that your JSF application will be using. so for example you have a bean that has a method that returns a collection of values that you need to populate a dropdown list. You would simply register this bean as a "managed bean" in your faces-config file and then you could
bind the dropdown list in your page to this method in your managed bean.
Hopefully this explanation makes sense from a high level point of view. The other thing to keep in mind is that JSF doesn't just toss a page your way. Instead, for each request it follows a well defined lifecycle which does a series a specific services such as updating server-side values, checking validation, invoking business logic etc. So at any point in the lifecycle you can - if you want - execute any custom code. This is done with what's called a PhaseListener.
Hope this helps - at least from a conceptual point of view..
Chapter 4 covers the managed bean facility in great detail by the way..
-Chris