myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig McClanahan" <craig...@apache.org>
Subject Re: how to execute one-time start-up code?
Date Fri, 18 May 2007 16:15:35 GMT
On 5/18/07, eric.jung@novartis.com <eric.jung@novartis.com> wrote:
>
>
> Hi,
>
> Calling FacesContext.getCurrentInstance() returns null from my own
> ServletContextListener.contextInitialized(), even though I've written the
> web.xml like so:
>
>         <listener>
>                 <listener-class>
> org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
>         </listener>
>
>         <listener>
>                 <!-- Our context listener must come AFTER MyFaces's
> ContextListener -->
>                 <listener-class>com.myco.MyServletContextListener
> </listener-class>
>         </listener>
>
> This is using JBoss 4.x. Is there any way to force MyFaces'
> ContextListener to execute first?


Ordering will not make any difference, because there never will be a
FacesContext at initialization time ... that only happens when a request
comes in.

However, you can still easilhy initialize application scope attributes,
because your contextInitialized() method gets a n indirect reference to the
ServletContext, so you can just store an attribute there.  Once you start
processing requests, these attributes will be pre-existing application scope
beans.

    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        Foo foo = new Foo(...);
        context.setAttribute("foo", foo);
    }

Craig

Mime
View raw message