2013-04-26 47 views
1

我可能对于OmniFaces太盲目,太新,并且在API中找不到检索辅助bean实例的基本方法。哪里可以找到这样一种方法?像这样的:OmniFaces中的getBackingBean()

public static Object getBackingBean(String name) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    Application app = context.getApplication(); 
    ValueExpression expression = app.getExpressionFactory() 
     .createValueExpression(context.getELContext(), String.format("#{%s}", name), Object.class); 
    return expression.getValue(context.getELContext()); 
} 

或者一个更有活力的版本,泛型:

public static <T> T getBackingBean(String name) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    Application app = context.getApplication(); 
    ValueExpression expression = app.getExpressionFactory() 
     .createValueExpression(context.getELContext(), String.format("#{%s}", name), Object.class); 
    return (T) expression.getValue(context.getELContext()); 
} 
+1

'''与''相同。 – 2013-04-26 13:41:36

回答

2

我们有一个几乎是这样的一种方法,但它可以评估(和获取)任何一种表达的不只是一个简化的根表达。

这是Faces.evaluateExpressionGet

您按如下方式使用它:

MyBean myBean = Faces.evaluateExpressionGet("#{myBean}"); 

随着MyBean是如定义如下:

@ViewScoped 
@ManagedBean 
public class MyBean { 
    // ... 
} 
相关问题