【问题标题】:getBackingBean() in OmniFacesOmniFaces 中的 getBackingBean()
【发布时间】:2013-04-26 12:44:57
【问题描述】:

我可能对OmniFaces 太盲目和太陌生,在API 中找不到基本方法来检索支持bean 实例。如果有这种方法,我在哪里可以找到这种方法?喜欢这个:

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());
}

【问题讨论】:

  • &lt;T extends Object&gt; 与&lt;T&gt; 相同。

标签: jsf omnifaces


【解决方案1】:

我们有一个几乎类似的方法,但它可以评估(并获取)任何类型的表达式,而不仅仅是简化的根表达式。

我是Faces.evaluateExpressionGet。

你按如下方式使用它:

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

MyBean 是例如定义如下:

@ViewScoped
@ManagedBean
public class MyBean {
    // ...
}

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多