JSF & Facelets Flow
Asked Answered
C

3

1

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!

Camelopardus answered 31/10, 2008 at 2:29 Comment(0)
G
2

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

Gaulin answered 31/10, 2008 at 9:49 Comment(0)
A
1

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!

Acherman answered 31/10, 2008 at 9:19 Comment(0)
S
0

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.

Sergent answered 31/10, 2008 at 3:9 Comment(2)
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.Evite
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?Sergent

© 2022 - 2024 — McMap. All rights reserved.