JSF 2 : Accessing managed bean's instance in the phase listener object?

12,507

You can access your managed beans via the ELContext/ELResolver. This is explained nicely in the MyFaces wiki (also works in vanilla JSF).

For example:

ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean = (NeededBean) FacesContext.getCurrentInstance().getApplication()
    .getELResolver().getValue(elContext, null, "neededBean");

See the MyFaces wiki entry for further explanation, and implementations for other JSF versions.

Your idea of using @PostConstruct is a good one. Consider changing your scope to something liked @ViewScoped, so the logic is executed everytime you navigate to that view.

Also, have a look at the PreRenderViewEvent (for JSF 2). This code is embedded in your facelet page:

<f:metadata>
<f:viewParam name="foo" value="#{bean.foo}"/>
<f:event type="preRenderView" listener="#{bean.doSomething}"/>
</f:metadata>

The f:event listener is executed before every page view.

Share:
12,507
Albert Gan
Author by

Albert Gan

http://albertkam.blogspot.com https://sites.google.com/site/tnhdhamma/

Updated on June 25, 2022

Comments

  • Albert Gan
    Albert Gan almost 2 years

    Is it possible to get a reference to the to-be-executed managedbean in the before-invokeApplication-phaselistener ?

    Before the invoke application phase, it should be clear which managedBean that is going to execute the method.

    For the sake of the example, assume there's 1 main manage bean to handle 1 jsf page.

    So what i need is basically :

    1. The user access the program from the menu
    2. Because it's accessed from the menu, the main manage bean's init() method gets called to initialize stuffs like preparing data, doing authorization checks
    3. Subsequent submits dont need to call the init() method anymore until it's reaccessed from the menu

    To implement the point #2, im thinking of intercepting one of the phases

    I've checked the API docs about getting the managed bean in the phases implementation, but i couldnt seem to find any.

    After typing this question, i realize i could do this in @PostConstruct or the managed bean's constructor, but that would do only at the first time the bean is constructed, and my need is to call the method everytime the jsf is being accessed from the menu.

    Any suggestions ?

    Regards,
    Albert Kam

  • BalusC
    BalusC over 13 years
    I don't see how that returns exactly the instance which is been involved in the action method. Your approach requires that the bean name is already known beforehand. By the way, I prefer the Application#evaluateExpressionGet() shorthand instead of ELResolver/ELContext. See also this answer.
  • Brian Leathem
    Brian Leathem over 13 years
    Well, if his phase listener is listening in the before invoke application phase, then the phase listener will instantiate the object instance. This same instance will be referred to in the application method (that get invoked during the invoke application phase), since the managed been is still in scope (same request). I answered the question of how to do it, but I don't advocate it as the best solution. I provided alternatives that seem better suited to achieving the end goal.