Question

Is there a way to get the current instance of a PhaseListener via el context or application context?

Was it helpful?

Solution

You can get the phase listeners attached to the UIViewRoot using <f:phaseListener> tag on a page like this:

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

It returns a list of the PhaseListener instances attached to this UIViewRoot instance.

If you want to get all the global phase listeners registered in a faces-config.xml file you can get them from the LifeCycle instance like this:

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

OTHER TIPS

You can get the current PhaseID in JSF 2.0 with:

PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top