PhaseListenerprogrammatically. I had originally tried to use a faces-config.xml to add the listener. I encountered a problem with an ear file which contained a war that contained a space. When Weblogic expanded the ear file it could not find the faces-config.xml since the path from expanded ear file had a space. Unfortunately, the program had too many dependencies to change to remove the space. So I decided to inject it. I was quite surprised to find that there was no annotation for PhaseListener in JSF 2.1.
Undaunted, I decided to figure it out. Admittedly, after I figured it out, I searched on Google, but everyone who had posted a solution did not a good example , or it was not quite as elegant as mine. I believe.
Specifically, my use case was to provide programmatic login where I use a PhaseListener to determine if the user is authenticated before restoring the view. This particular use case means that I must have the PhaseListener injected prior to responding to any requests. Fortunately, the JSF 2 API makes it very easy to do this.
The basic design is as follows:
- Use a
@ManagedBean(eager=true)annotation. The eager attribute forces the container to load the bean immediately rather than lazy loading.
- Use the
@ApplicationScopedannotation. This will allow us to set the PhaseListener for the whole application.
- Create a method which has a
@PostConstructannotation. This will allow us to set the listener immediately following the constructor.
Note: You will want to follow this design so that if there are any annotations, they will be processed prior to adding the
- Add the listener in the
The JSF 2 API does a great job of explaining, how to use the
LifecycleFactoryto get a reference to the
Lifecycleobject. This is the key to adding the PhaseListener. If you are an API developer, this is a great example of documentation for people who want to use your API.