【问题标题】:JSF <ui:debug> not showing CDI beansJSF <ui:debug> 不显示 CDI bean
【发布时间】:2013-11-17 23:06:59
【问题描述】:

当我将托管 bean 定义为 CDI bean (@Named) 时,ui:debug 弹出窗口不会显示它。如果我将定义更改为 JSF @ManagedBean,它会很好地显示在作用域变量中。 我需要做些什么来完成这项工作吗? 我正在使用 Mojarra 2.1。

【问题讨论】:

  • 你也给它一个范围吗?当您从 @ManagedBean 切换到 @Named 时,您是否将 JSF 范围切换为 CDI 范围?
  • 是的,两者都是 SessionScoped(分别在 javax.faces.bean 和 javax.enterprise.context 中)

标签: debugging jsf facelets cdi managed-bean


【解决方案1】:

CDI 托管 bean 不存储为请求/会话/应用程序范围的直接属性。它们在 CDI 上下文后面被抽象出来,而 CDI 上下文又依赖于实现(例如 Weld、OpenWebBeans 和其他),它们在范围中的引用方式。 &lt;ui:debug&gt; 不提供任何内置工具来显示活动的 CDI 托管 bean(还没有?)。

您最好的选择是手动获取它们。您可以为此使用以下实用程序方法(将在即将推出的OmniFaces 1.7 的Beans 实用程序类中提供):

public static Map<Object, String> getActiveReferences(BeanManager beanManager, Class<? extends Annotation> scope) {
    Map<Object, String> activeReferences = new HashMap<Object, String>();
    Set<Bean<?>> beans = beanManager.getBeans(Object.class);
    Context context = beanManager.getContext(scope);

    for (Bean<?> bean : beans) {
        Object reference = context.get(bean);

        if (reference != null) {
            activeReferences.put(reference, bean.getName());
        }
    }

    return Collections.unmodifiableMap(activeReferences);
}

你可以这样使用它:

@Inject
private BeanManager manager;

public void collect() {
    Map<Object, String> requestScopedBeans = Beans.getActiveReferences(manager, RequestScoped.class);
    // Map key represents the instance and map value represents the managed bean name, if any.

    // ...
} 

请记住,这是一项相对昂贵的工作。所以真的只用它来调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 2013-08-20
    • 2016-08-02
    • 2012-07-23
    • 2016-07-09
    • 2012-04-13
    • 2018-01-08
    • 2014-07-17
    相关资源
    最近更新 更多