2012-07-10 67 views

回答

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

Documentation UIViewRoot

Documentation LifeCycle

-1

您可以在JSF 2.0中獲得當前PhaseID:

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

你沒有回答這個問題。 OP詢問如何獲取當前註冊的PhaseListener,而不是如何獲取當前的階段ID,這確實太明顯了。 – BalusC 2012-09-13 18:42:45