JSF + backing bean: How to detect when the application first load
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 sessionB开发者_JAVA百科ean.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.
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;
}
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.
精彩评论