有没有办法通过el上下文或应用程序上下文获取PhaseListener的当前实例?
答案 0 :(得分:3)
您可以在页面上使用<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();
}
答案 1 :(得分:-1)
您可以使用以下命令获取JSF 2.0中的当前PhaseID:
PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();