Creating JSF phase listener

Posted by: Max Katz on 07/29/2008

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

  • Currently 3.4/5
  • 1
  • 2
  • 3
  • 4
  • 5
3.4 rating out of 22 votes

About Max Katz

Max Katz

Max Katz is a Senior Systems Engineer at Exadel. He has been helping customers jump-start their RIA development as well as providing mentoring, consulting, and training. Max is a recognized subject matter expert in the JSF developer community. He has provided JSF/RichFaces training for the past three years, presented at many conferences, and written several published articles on JSF-related topics. Max also leads Exadel's RIA strategy and writes about RIA technologies in his blog, http://mkblog.exadel.com. He is an author of "Practical RichFaces" book (Apress). Max holds a BS in computer science from the University of California, Davis.

More About Max »

NFJS, the Magazine

August Issue Now Available
  • Google Your Persistent Domain Model
    by John Griffin
  • Get Cooking in the Cloud with Chef, Part 2
    by Michael Nygard
  • Making Java Bearable with Guava
    by Daniel Hinojosa
  • HTML 5 Update
    by Brian Sletten
Learn More »