views:

647

answers:

3

I have a dynamic Facelets page that needs to show information from database when the page loads. At this point in the flow, there have not been any form submissions. Every JSF example I can find only shows a form submission with dynamic results on the next page.

Every call I make to our database is currently takes place after an action has been triggered by a form submission. Where should this code go if there hasn't been a form submission, and how do I trigger it? A code snippet would really help me out!

A: 

You write (with my emphasis added):

Every call I make to our database is currently takes place after an action has been triggered by a form submission. Where should this code go if there hasn't been a form submission, and how do I trigger it? A code snippet would really help me out!

It sounds to me that you want to retrieve information from the database prior to form submission.

It seems to me that you want to make an Ajax call to query the database. The Ajax call can fire on a different event than the form submisson event. This will probably entail using Javascript rather than the Faces framework.

Alan
Sorry, I can see why you'd think that. My bad. I'm looking to set these values as the page loads, without any user interaction.
do you mean staged loading after the user brings up the form, right? why can't the event that causes the form load up start firing additional ajax calls?
Alan
+1  A: 

If you're using Spring integration (see here also), it's easy.

In your backing bean, simply use something like:

public class BackingBean implements InitializingBean
{
    public void afterPropertiesSet()
    {
        loadInitialData();
    }
}

If you're not integrating with Spring there are two options:

  1. Load the initial data in the class constructor;
  2. In your faces-config.xml, you can set properties to be injected. Properties are guaranteed to be set in the order they're specified in the config file. So, just create a dummy property and then within that method load up your default data. i.e. create a method public void setLoaded(boolean loaded) { loadInitialData(); }, and in your faces-config.xml have 'loaded' being set as a property on that backing bean.

Hope this is all clear!

Phill Sacre
+2  A: 

You should be able to do your initialization work in the constructor (or lazily in one of your accessors) of your managed bean.

Jack Leow