如何以编程方式注册JSF托管bean? [英] How to register a JSF managed bean programmatically?

查看:73
本文介绍了如何以编程方式注册JSF托管bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式(从Servlet init()内部)将Managed Bean类注册/添加到应用程序范围. 如何使用JSF 1.2做到这一点?

解决方案

Servlet init()

中的

因此,它涉及一个非JSF请求. FacesContext#getCurrentInstance()在这里将返回null,因此在这里对您没有用.

很高兴知道JSF应用程序范围的托管Bean基本上存储为ServletContext的属性.在init()方法中,继承的

就是这样. #{managedBeanName}可以在JSF中使用.

I'd like to register/add a Managed Bean class programmatically (from within a Servlet init()) into application scope. How can I do that with JSF 1.2?

from within a Servlet init()

So, it concerns a non-JSF request. The FacesContext#getCurrentInstance() would return null here, so it's of no use for you here.

It's good to know that JSF application scoped managed beans are basically stored as an attribute of the ServletContext. In the init() method you've the ServletContext at your hands by the inherited getServletContext() method. So, the following should do:

@Override
public void init() {
    getServletContext().setAttribute("managedBeanName", new BackingBean());
}

That's it. It'll be available in JSF by #{managedBeanName}.

这篇关于如何以编程方式注册JSF托管bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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