This is a bit of an odd one. A phase listener will run twice only if you tell it! That’s right a computer only does what you tell it!
One common problem with JSF is that you install a phase listener and it runs twice for each phase (that is 4 times for each phase – two invocations of the beforePhase method and two invocations of the afterPhase method). Why and how to solve it?
If you look at the JSF specification (JSF 1.1) on section 10.3.2 the application startup behavior is defined, and it says that the JSF must process zero or more configuration resources using the following algorithm:
- All resources named ‘META-INF/faces-config.xml’ in the ServletContext resource path of this webapp.
- Check for the ‘javax.faces.CONFIG_FILES’ initialization parameter (that is a context parameter in the web.xml file). If exist treat it as a comma-delimited list of context relative resource paths. And load each resource.
- Check for the existing of a web application configuration resource named ‘/WEB-INF/faces-config.xml’. And load it.
Now when we first start a new JSF webapp application we create it with a configuration resource named WEB-INF/faces-config.xml, and then at the web.xml file we announce this configuration resource: