In particular, it would be nice if there were a way
to ensure that a managed bean was loaded and initialized before
the page was invoked.Here is my scenario. You have a view that needs to
display data, so your managed bean needs to perform database operations,
probably in your getters:
There are a couple of ways to do this. Right now. In JSF 1.1, you
could use an application scoped PhaseListener that has an awareness of
the meaning of each viewId in your app. You could have this
PhaseListener take action on the beforePhase of the
PhaseId.RENDER_RESPONSE phase. When beforePhase happens, you could
examine the viewId of the current UIViewRoot on the FacesContext and
take action accordingly. Of course, this causes an undesirable coupling
between the PhaseListener and your viewIds, but this can be mitigated
somewhat.
Another approach is to place a simple managed bean get at the top of
the page that returns the empty string, something like this:
<h:outputText value="#{lifecycleBean.start}" />
This is something of a hack, but if you put this as the first thing
in your f:view it's guaranteed to be evaluated before anything else in
the page. You could then use the getStart() method to kick off any
per-page initialization.
On a related note, one thing I want to push for JSF 1.2 is the use of
@PostConstruct and @PreDestroy annotations on managed beans. Any no
argument managed bean method annotated with @PostConstruct will be
called after the bean is instantiated, but before it is placed in
service. Likewise, any no argument managed bean method annotated with
@PreDestroy will be called before the bean goes out of scope. This
isn't guaranteed to be in 1.2 yet, but I'm hoping it will be.
Ed
JSR-252 spec co lead.