Pages

Monday, May 07, 2012

I have to do some JSF training in the next couple of weeks for some of the staff at the company where I work. The learning curve for JSF has often been called a vertical wall. In part this is due to the JSF Lifecycle. The JSF Lifecycle has a number of phases, and before and after each phase there is an extension point where we can intervene in the process using a PhaseListener.

Since the PhaseListener intercepts the process, you can use it to learn the phases. The PhaseListener below is an example of one that reports on all PhaseEvents. You should modify it to learn what occurs during each phase.

Sunday, May 06, 2012

I needed to redirect a user to a login page the other day, and solved it using a PhaseListener. Subsequently, I had another need to do a page redirection based on a value in the HttpSession. I thought I would share the general idea of how to do it with you.

The PhaseListener allows us to take action at various integration points in the JSF lifecycle. The most likely place you will want to handle an internal redirection, is before the view is restored. We use the JSF NavigationHandler to do the work for us.

PageRedirectionPhaseListener.java

Saturday, May 05, 2012

I had a use case where I needed to use a PhaseListener programmatically. 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 @ApplicationScoped annotation. This will allow us to set the PhaseListener for the whole application.

Create a method which has a @PostConstruct annotation. 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 PhaseListener.

Add the listener in the @PostConstruct method.

The JSF 2 API does a great job of explaining, how to use the LifecycleFactory to get a reference to the Lifecycle object. 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.