views:

318

answers:

3

I have a page on which I show a list of objects which are loaded from a webservice. This may take a while. Now I'd like to do it the Ajax way and first show the page and then load the list. While the list is loading a animated should be shown. Can anybody give me an example how to do that with JSF/ICEFaces? Thanks.

A: 

Ok, solved it myself.

Here's how I'm doing it:

in the Managed Bean's constructor I'm calling a method which creates and starts a new thread. This thread loads the objects. As soon as the objects are there the 'loading' flag of my bean is set to false and

PersistentFacesState.getInstance().renderLater();   

is called. 'loading' is initially set to false.

That's the method for loading the objects in async mode:

 private void asyncLoading() {
            final MyBackingBean bean = this;
            final String userName = CallerContextUtil.getCurrentUserCompany();
            new Thread() {
                @Override
                public void run() {
                    bean.setLoading(true); 
                    PersistentFacesState.getInstance().renderLater();       
                    load(userName );                
                    bean.setLoading(false);             
                    PersistentFacesState.getInstance().renderLater();                   
                }
            }.start();      
        }

On the view I'm showing or hiding an animated loading images on the state of the loading flag.

I don't know if that is the best or even a good solution. Comments are welcome.

hubertg
A: 

It would be better to use the SessionRenderer. Have a look at my ICEfaces book examples (http://icecube-on-icefusion.googlecode.com/) and search for the progressDialog tag for an example. Or page 244ff in the book.

rainwebs
What's wrong with the PersistentFacesState and what makes the SessionRenderer better? Which problems I can into with my approach? It seems to work ...
hubertg
A: 

It is strongly discouraged to create new threads in a J2EE server. It's server's job to manage threads.

You can ask for the submission of an icefaces form using ajax when your page is loaded, that should do the trick.

Awano
How can I ask "for te submission of an icefaces from using ajax"? Thanks!
hubertg
Well the idea can be to put an hidden form on the page that you submit when the page loads. Every JS framework has a feature like this (equivalent of body onload="form.submit();").And from this point it's as if you asked for an ajax refresh thanks to a form submission.This is very basic and may not be adequate for more complex cases, but it has the advantage of simplicity. If you want to go for more complex things, better use the method from rainwebs, it seems to be more in line with ICEFaces way of doing.
Awano