Hello there.

Please bear with me in this description.

I'm trying to get my request-scoped beans to be initialized (fetch stuff
from database, etc.) whenever they do not originate from a postback (and
keeping the state during postbacks using t:saveState). I've implemented a
PhaseListener which checks for the proper conditions I want to initialize my
beans in and invokes a method from the bean interface my beans implement.

This is my implementation of said PhaseListener:

public void beforePhase(PhaseEvent event) {

   if (event.getPhaseId().equals(PhaseId.APPLY_REQUEST_VALUES) ||
event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {

       GenericBean bean = (GenericBean) FacesContext.getCurrentInstance
().getApplication().getVariableResolver().resolveVariable(
FacesContext.getCurrentInstance(), "pesquisaFichaBean");
       if (bean != null && !bean.isPostback()) {
           logger.info("Initializing bean: " + bean);
           bean.initialize();
       }
   }
}


However I have two problems with this approach.

The first problem has to do with having to use the bean names explicitly
("pesquisaFichaBean" in this example). Is there any way I can know the names
of the registered beans? Or is there another alternative I'm not seeing?


The second problem is as follows. By now I have a couple of beans that I use
in my application. So, for this to work, I have to repeat what I've shown
above for the various beans. The problem is that the resolveVariable method
never returns null - even if I have not yet instantiated any bean with that
particular name. It seems to me that, since no bean yet exists, it is
instantiated (as it would be if it were referenced in a xhtml page).

Which means that everytime I enter a new page (no postback), all my
instantiated beans are initialized and the remaining are instantiated on the
spot and initialized as well!!! This obviously brings me back to the problem
of accessing the database for nothing.

I hope I explained the situation well, here's an example: suppose some pages
require "beanA", others "beanB". When I enter a page which references
"beanA" but does not reference "beanB", both will be initialized! The
difference is beanA will be used and beanB will.

What solutions are there for this? Is there any way I can look into the
variables without triggering their instantiation?

Reply via email to