views:

45

answers:

2

How do I do something like this
Inside JSF file, list.xhtml

 <p:dataTable value="#{document.drawings}" var="item">
     //document is the backing bean that has method getDrawings() that return list of item
 </p:dataTable>  

Inside my backing bean, document.java

 List<Drawing> drawings;
 ...
 public void List<SelectItem> getDrawings(){
      if(application first load){
           return sessionBean.getAllDrawings();
      }else{
           return drawings;
      }
 }

So the logic is that if the application first load, then load every thing from the datasource, by accessing method getAllDrawings() inside session bean, otherwise return drawings which is the list of Drawing that has been manipulate by some ajax method.

+1  A: 

Declare it as an application scoped bean and put the desired application-startup-initialization logic in its constructor. You can if necessary inject it as <managed-property> (or if you're already on JSF 2.0, as @ManagedProperty) in any other request/session scoped bean.

An application scoped bean is created only once and shared among all sessions/requests during webapplication's lifetime.

BalusC
Can u explain a bit about why do I need `@ManagedProperty`?
Harry Pham
It's useful if you want to access *other* bean from inside a bean. I can imagine that you don't want to use an application scoped bean to handle form submits and so on, but rather a request scoped bean.
BalusC
+1  A: 

You can have a

@PostConstruct
public void init() { 
      drawings = loadDrawings();
}

But you can also have the so-called "lazy-loading". I.e.:

public void List<SelectItem> getDrawings(){
      if(drawings == null) {
           drawings = sessionBean.getAllDrawings();
      }
      return drawings;
}
Bozho
I try your method, both of them work great. TY. However, I got one more question. So when the page first loads, it loads let say 6 drawings. Then i filters it down to 4 drawings. I click on 1 of the drawing to take me to another page. However, when I click the back button, it go back to the first page, but now it load 6 drawings. Is there a way that I can go back to the original page where I filter down to 4 drawings
Harry Pham
That's quite a different question - ask a new one, providing the relevant code
Bozho