开发者

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

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 ne开发者_如何学Ced 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜