wood burning stoves 2.0*
The moose likes JSF and the fly likes How to adjust Application at start-up? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to adjust Application at start-up?" Watch "How to adjust Application at start-up?" New topic
Author

How to adjust Application at start-up?

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I want to install my own NavigationHandler into the javax.faces.application.Application object. I know that I can do this using a PhaseListener, but I think this operation is something that could be done just once. I was wondering what the best practice was for the placement of code to alter JSF that could be run at start-up, so to speak? Does the JSF provide any special hooks for this?


There is no emoticon for what I am feeling!
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
>How to adjust Application at start-up?
ServletContextListener
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Daniel Prene:
>How to adjust Application at start-up?
ServletContextListener


That doesn't work because, as the API states:

All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

I need to run code *after* JSF has been configured. For example,

FacesContext context = FacesContext.getCurrentInstance();

will return null in the ServletContextListener's contextInitialized method.

I have found a way that almost works for me, and might work for others.
You can specify what class you want instantiated used as your navigation handler in your faces-config file:

Ideally, I'd like my navigation handler to take whatever is the default navigation handler and decorate it, but with this approach, I'm forced to hardwire in class org.apache.myfaces.application.NavigationHandlerImpl, for example.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Oh happy day! I found a solution and it's simple. Let me summarize my problem: I wanted to write a custom NavigationHandler backed by whatever is the default navigation handler:

I wanted to install this component at JSF startup, so I added to my faces-config:

The problem was how to set the base navigation handler -- it only needs to be done once, but the only place I could think of doing it was from a PhaseListener. Then I came across an example of a custom navigation handler that didn't define a default constructor -- my mistake was that I had thought that was necessary. So I changed my code to:

And this new constructor is being successfully called because of the same config file reference I listed a few lines ago.
[ March 07, 2006: Message edited by: Jeff Albertson ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to adjust Application at start-up?