Creating JSF phase listener

Understanding the JSF life cycle is very important for understanding how JSF works. It’s also very useful for debugging. For example, if you pass phases 1-3 and then jump to phase 6, this usually means there were conversion/validation errors. Knowing what phases were passed, you need to create a simple phase listener. Here is the super quick way to create a phase tracker.

First, creating the actual phase listener:

package lifecycle;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;

public class PhaseTracker implements
	javax.faces.event.PhaseListener {

	public void afterPhase(PhaseEvent event) {
		FacesContext.getCurrentInstance().getExternalContext().log("AFTER - "+
                                  event.getPhaseId());
	}
	public void beforePhase(PhaseEvent event) {
		FacesContext.getCurrentInstance().getExternalContext().log("BEFORE - "+
                                 event.getPhaseId());

	}
	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}
}

Second and the last step is to register the listener in JSF configuration file:


    lifecycle.PhaseTracker

That’s it. Restart the server and look for addition output in the console. It should look like this:

INFO: BEFORE - RESTORE_VIEW 1
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - RESTORE_VIEW 1
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: BEFORE - APPLY_REQUEST_VALUES 2
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - APPLY_REQUEST_VALUES 2
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: BEFORE - PROCESS_VALIDATIONS 3
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - PROCESS_VALIDATIONS 3
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: BEFORE - UPDATE_MODEL_VALUES 4
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - UPDATE_MODEL_VALUES 4
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: BEFORE - INVOKE_APPLICATION 5
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - INVOKE_APPLICATION 5
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: BEFORE - RENDER_RESPONSE 6
Jul 29, 2008 10:15:30 AM org.apache.catalina.core.ApplicationContext log
INFO: AFTER - RENDER_RESPONSE 6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s