JSF Managed Bean自动创建? [英] JSF Managed Bean auto-create?

查看:170
本文介绍了JSF Managed Bean自动创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以自动创建JSF托管bean?

Is it possible to have a JSF managed bean be automatically created?

例如,我有几个会话范围的bean。有时需要在代码中访问这些实例(而不仅仅是在JSF中),这可以通过以下方式完成:

For example I have several session scoped beans. Sometimes it becomes necessary to access these instances in code (rather than just in JSF) this is done by:

PageBean pageBean = (PageBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("pages");

但是,如果没有访问过调用#{pages}的页面,则此解析为null ...无论如何,当范围开始时,JSF是否可以创建一个bean?那么在这种情况下,理想情况下,当用户会话开始时,'pages'bean会立即在会话中实例化吗?

However if no page has already been visited which calls to '#{pages}' this resolves to null ... is there anyway to get JSF to create a bean when the scope 'begins'? So in this case ideally when a user session begins the 'pages' bean would be instantiated in the session immediately?

推荐答案

使用< a href =http://java.sun.com/javaee/5/docs/api/javax/faces/application/Application.html#evaluateExpressionGet%28javax.faces.context.FacesContext,%20java.lang.String,% 20java.lang.Class%29rel =noreferrer> 应用程序#evaluateExpressionGet() 。它还会在尚未完成时创建bean。

Use Application#evaluateExpressionGet() instead. It will create bean when not done yet.

FacesContext context = FacesContext.getCurrentInstance();
Bean bean = (Bean) context.getApplication().evaluateExpressionGet(context, "#{bean}", Bean.class);

其中bean是托管bean name和 Bean.class 是适当的支持bean类。

Where "bean" is the managed bean name and Bean.class is the appropriate backing bean class.

如果需要,你可以将它包装在一个帮助器中方法,以便不必进行投射(JSF男孩没有利用泛型和 evaluateExpressionGet 中的 Class 参数) :

You can if necessary wrap this up in a helper method so that casting is unnecessary (the JSF boys didn't take benefit of generics and the Class parameter in evaluateExpressionGet):

public static <T> T findBean(String managedBeanName, Class<T> beanClass) {
    FacesContext context = FacesContext.getCurrentInstance();
    return beanClass.cast(context.getApplication().evaluateExpressionGet(context, "#{" + managedBeanName + "}", beanClass));
}

可以用作:

Bean bean = findBean("bean", Bean.class);

或者没有类型,但是带有 @SuppressWarnings

Or without the type, but with a @SuppressWarnings:

@SuppressWarnings("unchecked")
public static <T> T findBean(String managedBeanName) {
    FacesContext context = FacesContext.getCurrentInstance();
    return (T) context.getApplication().evaluateExpressionGet(context, "#{" + managedBeanName + "}", Object.class);
}

可以用作:

Bean bean = findBean("bean");






更新:以上是按照JSF 1.2的具体方式。这是JSF 1.1或更早版本的方式,使用当前已弃用的 Application#createValueBinding()


Update: the above is by the way JSF 1.2 specific. Here's the way for JSF 1.1 or older, using the currently deprecated Application#createValueBinding():

FacesContext context = FacesContext.getCurrentInstance();
Bean bean = (Bean) context.getApplication().createValueBinding("#{bean}").getValue(context);

这篇关于JSF Managed Bean自动创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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