JSF 2:在阶段侦听器对象中访问托管bean的实例? [英] JSF 2 : Accessing managed bean's instance in the phase listener object?

查看:58
本文介绍了JSF 2:在阶段侦听器对象中访问托管bean的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在before-invokeApplication-phaselistener中引用要执行的托管bean?

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

在调用应用程序阶段之前,应清楚将要执行该方法的ManagedBean.

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

在此示例中,假设有1个main manage bean来处理1个jsf页面.

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. 用户从菜单访问程序
  2. 因为可以从菜单中访问它,所以会调用主管理bean的init()方法来初始化诸如准备数据,进行授权检查之类的事情
  3. 后续提交不再需要调用init()方法,直到从菜单中重新访问它为止

要实现第二点,我想拦截其中一个阶段

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

我已经检查了有关在阶段实现中获取托管bean的API文档,但似乎找不到任何内容.

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

键入此问题后,我意识到我可以在@PostConstruct或托管bean的构造函数中执行此操作,但这仅在第一次构建bean时才能执行,我的需要是每次jsf被调用时都调用该方法.从菜单访问.

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.

有什么建议吗?

关于,
阿尔伯特·金

Regards,
Albert Kam

推荐答案

您可以通过ELContext/ELResolver访问托管bean.在 MyFaces Wiki 中也有很好的解释(也适用于香草JSF).

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

例如:

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

有关进一步的说明以及其他JSF版本的实现,请参见 MyFaces Wiki条目.

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

您使用@PostConstruct的想法很不错.考虑将范围更改为类似@ViewScoped的范围,以便每次您导航到该视图时都会执行逻辑.

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.

另外,请查看 PreRenderViewEvent (对于JSF 2).此代码已嵌入到您的Facelet页面中:

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>

f:event侦听器在每次页面浏览之前执行.

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

这篇关于JSF 2:在阶段侦听器对象中访问托管bean的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆