Hi.
Is it possible to implement a PhaseListener on a backing bean? I tried to do
this but could not
get it to work.
I'd like to be able to trace the phases as a page loads. Is there some
standard way to know what
phase of the component life cycle has been activated?
What I did was implementing a small PhaseListener which dispatches phase
events among session attributes. Works fine for me. I simply implement
PhaseListener in my session-scoped bean and it gets notified about
phases. The same could be done for page and application context, I only
needed that for the session context.
Source code attached.
Bye.
/lexi
package de.disy.preludio2.faces.event;
import java.util.Enumeration;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;
public class SessionDispatchingPhaseListener implements PhaseListener {
public void afterPhase(PhaseEvent event) {
final int phaseId = event.getPhaseId().getOrdinal();
final HttpSession session = (HttpSession) event
.getFacesContext()
.getExternalContext()
.getSession(false);
if (session != null) {
for (Enumeration attributeNames = session.getAttributeNames();
attributeNames
.hasMoreElements();) {
final String attributeName = (String) attributeNames.nextElement();
final Object value = session.getAttribute(attributeName);
if (value instanceof PhaseListener) {
final PhaseListener phaseListener = (PhaseListener) value;
int targetPhaseId = phaseListener.getPhaseId().getOrdinal();
if (phaseId == targetPhaseId || PhaseId.ANY_PHASE.getOrdinal() ==
targetPhaseId) {
phaseListener.afterPhase(event);
}
}
}
}
}
public void beforePhase(PhaseEvent event) {
final int phaseId = event.getPhaseId().getOrdinal();
final HttpSession session = (HttpSession) event
.getFacesContext()
.getExternalContext()
.getSession(false);
if (session != null) {
for (Enumeration attributeNames = session.getAttributeNames();
attributeNames
.hasMoreElements();) {
final String attributeName = (String) attributeNames.nextElement();
final Object value = session.getAttribute(attributeName);
if (value instanceof PhaseListener) {
final PhaseListener phaseListener = (PhaseListener) value;
int targetPhaseId = phaseListener.getPhaseId().getOrdinal();
if (phaseId == targetPhaseId || PhaseId.ANY_PHASE.getOrdinal() ==
targetPhaseId) {
phaseListener.beforePhase(event);
}
}
}
}
}
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}