【问题标题】:JSF 2 -- Get instance of a PhaseListener from a managed beanJSF 2 -- 从托管 bean 获取 PhaseListener 的实例
【发布时间】:2012-07-10 20:57:28
【问题描述】:

有没有办法通过 el 上下文或应用程序上下文获取 PhaseListener 的当前实例?

【问题讨论】:

    标签: jsf jsf-2 phaselistener


    【解决方案1】:

    您可以在这样的页面上使用<f:phaseListener> 标签获取附加到 UIViewRoot 的阶段侦听器:

    List<PhaseListener> phaseListeners = FacesContext.getCurrentInstance().getViewRoot().getPhaseListeners();
    

    它返回附加到此 UIViewRoot 实例的 PhaseListener 实例的列表。

    如果您想在 faces-config.xml 文件中注册所有全局阶段侦听器,您可以从 LifeCycle 实例中获取它们,如下所示:

    FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);    
    LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    Iterator<String> lifecycleIds = lifecycleFactory.getLifecycleIds(); 
    while (lifecycleIds.hasNext()) { 
        String lifecycleId = lifecycleIds.next(); 
        Lifecycle lifecycle = lifecycleFactory.getLifecycle(lifecycleId);
        PhaseListener[] phaseListeners= lifecycle.getPhaseListeners();
    }
    

    Documentation UIViewRoot

    Documentation LifeCycle

    【讨论】:

      【解决方案2】:

      您可以通过以下方式在 JSF 2.0 中获取当前 PhaseID:

      PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
      

      【讨论】:

      • 你没有回答这个问题。 OP是问如何获取当前注册的PhaseListeners,而不是如何获取当前阶段ID,这确实太明显了。
      猜你喜欢
      • 2013-09-06
      • 2012-11-08
      • 1970-01-01
      • 2014-09-29
      • 2014-04-20
      • 2011-03-29
      • 2012-11-09
      • 1970-01-01
      • 2013-02-10
      相关资源
      最近更新 更多