FAQ
Hi,

This is a general JSF question; probably not MyFaces-specific. Can anyone
tell me how to get one-time initialization code executed during
application startup? With Spring and Struts, this was really easy to do.
With JSF, it's not obvious to me how to do it.

Thanks for any suggestions,
Eric

_________________________

CONFIDENTIALITY NOTICE

The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.

Search Discussions

  • Cagatay Civici at May 18, 2007 at 1:33 pm
    How about a servlet context listener?
    On 5/18/07, eric.jung@novartis.com wrote:


    Hi,

    This is a general JSF question; probably not MyFaces-specific. Can anyone
    tell me how to get one-time initialization code executed during application
    startup? With Spring and Struts, this was really easy to do. With JSF, it's
    not obvious to me how to do it.

    Thanks for any suggestions,
    Eric

    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure under
    applicable law. If the reader of this message is not the intended recipient,
    or the employee or agent responsible for delivery of the message to the
    intended recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited. If you
    have received this communication in error, please notify the sender
    immediately by e-mail and delete the material from any computer. Thank you.
  • Eric Jung at May 18, 2007 at 2:24 pm
    Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
    application-scoped managed beans in this code, the listener must execute
    after org.apache.myfaces.webapp.StartupServletContextListener. Is the best
    way to do this to subclass
    org.apache.myfaces.webapp.StartupServletContextListener, specify my
    subclass as the sole <listener-class> in web.xml (instead of after
    org.apache.myfaces.webapp.StartupServletContextListener), call
    super.contextInitialized() in my contextInitialized(), then execute my
    code? I'm not aware of a way to specify <listener-class/> order in
    web.xml.

    Thanks agan,
    Eric
    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.
  • Cagatay Civici at May 18, 2007 at 2:43 pm
    Hi,

    I'm not sure if there's a statement about servlet specification about
    listener order but usually containers invoke listeners using the declaration
    order during startup and in reverse order when the app shuts down.

    I'd go with my own listener rather because the myfaces listener is shipped
    in a taglib(although containers like jetty 5.1.2 does not support it) and
    subclassing it may cause duplicate invocations.

    Regards,

    Cagatay
    On 5/18/07, eric.jung@novartis.com wrote:


    Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
    application-scoped managed beans in this code, the listener must execute
    after org.apache.myfaces.webapp.StartupServletContextListener. Is the best
    way to do this to subclass
    org.apache.myfaces.webapp.StartupServletContextListener, specify my
    subclass as the sole <listener-class> in web.xml (instead of after
    org.apache.myfaces.webapp.StartupServletContextListener), call
    super.contextInitialized() in my contextInitialized(), then execute my
    code? I'm not aware of a way to specify <listener-class/> order in web.xml
    .

    Thanks agan,
    Eric
    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure under
    applicable law. If the reader of this message is not the intended recipient,
    or the employee or agent responsible for delivery of the message to the
    intended recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited. If you
    have received this communication in error, please notify the sender
    immediately by e-mail and delete the material from any computer. Thank you.
  • ::SammyRulez:: at May 18, 2007 at 2:43 pm
    can't you do it in the bean constructor?

    2007/5/18, eric.jung@novartis.com <eric.jung@novartis.com>:
    Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
    application-scoped managed beans in this code, the listener must execute
    after
    org.apache.myfaces.webapp.StartupServletContextListener. Is
    the best way to do this to subclass
    org.apache.myfaces.webapp.StartupServletContextListener,
    specify my subclass as the sole <listener-class> in web.xml (instead of
    after
    org.apache.myfaces.webapp.StartupServletContextListener),
    call super.contextInitialized() in my contextInitialized(), then execute my
    code? I'm not aware of a way to specify <listener-class/> order in web.xml.

    Thanks agan,

    Eric
    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure under
    applicable law. If the reader of this message is not the intended recipient,
    or the employee or agent responsible for delivery of the message to the
    intended recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited. If you
    have received this communication in error, please notify the sender
    immediately by e-mail and delete the material from any computer. Thank you.

    --
    ::SammyRulez::
    http://www.kyub.com/blog/
    -----------------------------------------------------------------
    La programmazione è per un terzo interpretazione e per due terzi ispirazione.
    E per un terzo mistificazione
  • Eric Jung at May 18, 2007 at 2:47 pm
    Yes, but how to invoke bean constructor at startup?





    "::SammyRulez::" <sammyrulez@gmail.com>
    05/18/2007 10:43 AM
    Please respond to
    "MyFaces Discussion" <users@myfaces.apache.org>


    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc

    Subject
    Re: how to execute one-time start-up code?






    can't you do it in the bean constructor?

    2007/5/18, eric.jung@novartis.com <eric.jung@novartis.com>:
    Excellent idea. Thanks for the reply, Cagatay. Since I need to
    initialize
    application-scoped managed beans in this code, the listener must execute
    after
    org.apache.myfaces.webapp.StartupServletContextListener. Is
    the best way to do this to subclass
    org.apache.myfaces.webapp.StartupServletContextListener,
    specify my subclass as the sole <listener-class> in web.xml (instead of
    after
    org.apache.myfaces.webapp.StartupServletContextListener),
    call super.contextInitialized() in my contextInitialized(), then execute my
    code? I'm not aware of a way to specify <listener-class/> order in web.xml.
    Thanks agan,

    Eric
    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure under
    applicable law. If the reader of this message is not the intended
    recipient,
    or the employee or agent responsible for delivery of the message to the
    intended recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited. If you
    have received this communication in error, please notify the sender
    immediately by e-mail and delete the material from any computer. Thank
    you.
    >


    --
    ::SammyRulez::
    http://www.kyub.com/blog/
    -----------------------------------------------------------------
    La programmazione è per un terzo interpretazione e per due terzi
    ispirazione.
    E per un terzo mistificazione


    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.
  • Cagatay Civici at May 18, 2007 at 2:47 pm

    Calling FacesContext.getCurrentInstance() returns null from my own
    ServletContextListener.contextInitialized(), even though I've written the
    web.xml like so

    There won't be any facescontext during startup, it's associated with a
    particular request so since there's no request during startup it'll be null.

    Eric what are you trying to do in this listener, I'm sure there's another
    way to achieve this without touching FacesContext.

    Cagatay
  • Eric Jung at May 18, 2007 at 3:10 pm
    What I'm trying to do is cache database data in a bunch of managed beans
    at startup. This data rarely changes, so it doesn't make sense to run the
    queries again and again. I've seen other people on the list do it this
    way:

    <h:commandLink
    actionListener="#{myManagedBean.init}"
    action="#{foo.bar}">

    and although this would work, I have 4 problems with this approach:

    1. I have *many* managed beans that need to be init'd, and since I think
    you can only have one actionListener per command, I'd need a "container"
    or "wrapper" to call each and every init.
    2. This init code would have to be sprinkled all over many JSF page (code
    bloat; harder to maintain if a change needs to be done)
    3. I'd need to write extra logic to prevent these managed beans from being
    initialized twice. Although this is easy and small to write, it's
    extraneous code. More code = more chance for bugs.
    4. Even though I'd have logic in each managed beans to prevent them from
    being initialized twice, the init() methods are continually called. This
    uses needless CPU cycles.

    You might suggest a static block in each and every bean and, while this
    would work, it's less than elegant.

    Thanks again for any ideas,
    Eric





    "Cagatay Civici" <cagatay.civici@gmail.com>
    05/18/2007 10:47 AM
    Please respond to
    "MyFaces Discussion" <users@myfaces.apache.org>


    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc

    Subject
    Re: how to execute one-time start-up code?






    Calling FacesContext.getCurrentInstance() returns null from my own
    ServletContextListener.contextInitialized(), even though I've written the
    web.xml like so

    There won't be any facescontext during startup, it's associated with a
    particular request so since there's no request during startup it'll be
    null.

    Eric what are you trying to do in this listener, I'm sure there's another
    way to achieve this without touching FacesContext.

    Cagatay



    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.
  • Eric Jung at May 18, 2007 at 2:42 pm
    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?





    "Cagatay Civici" <cagatay.civici@gmail.com>
    05/18/2007 09:33 AM
    Please respond to
    "MyFaces Discussion" <users@myfaces.apache.org>


    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc

    Subject
    Re: how to execute one-time start-up code?






    How about a servlet context listener?

    On 5/18/07, eric.jung@novartis.com wrote:

    Hi,

    This is a general JSF question; probably not MyFaces-specific. Can anyone
    tell me how to get one-time initialization code executed during
    application startup? With Spring and Struts, this was really easy to do.
    With JSF, it's not obvious to me how to do it.

    Thanks for any suggestions,
    Eric

    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.


    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.
  • Craig McClanahan at May 18, 2007 at 4:16 pm

    On 5/18/07, 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
  • Eric Jung at May 18, 2007 at 7:15 pm
    That worked. Thanks, Craig.





    "Craig McClanahan" <craigmcc@apache.org>
    Sent by: craigmcc@gmail.com
    05/18/2007 12:16 PM
    Please respond to
    "MyFaces Discussion" <users@myfaces.apache.org>


    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc

    Subject
    Re: how to execute one-time start-up code?








    On 5/18/07, 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


    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.
  • Eric Jung at May 18, 2007 at 2:46 pm
    Actually, I should also have asked: at what point is it safe to call
    FacesContext.getCurrentInstance()? After
    org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized()
    is complete?





    Eric Jung/X/PH/Novartis
    05/18/2007 10:42 AM

    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc
    "MyFaces Discussion" <users@myfaces.apache.org>
    Subject
    Re: how to execute one-time start-up code?





    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?





    "Cagatay Civici" <cagatay.civici@gmail.com>
    05/18/2007 09:33 AM
    Please respond to
    "MyFaces Discussion" <users@myfaces.apache.org>


    To
    "MyFaces Discussion" <users@myfaces.apache.org>
    cc

    Subject
    Re: how to execute one-time start-up code?






    How about a servlet context listener?

    On 5/18/07, eric.jung@novartis.com wrote:

    Hi,

    This is a general JSF question; probably not MyFaces-specific. Can anyone
    tell me how to get one-time initialization code executed during
    application startup? With Spring and Struts, this was really easy to do.
    With JSF, it's not obvious to me how to do it.

    Thanks for any suggestions,
    Eric

    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.


    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.


    _________________________

    CONFIDENTIALITY NOTICE

    The information contained in this e-mail message is intended only for the
    exclusive use of the individual or entity named above and may contain
    information that is privileged, confidential or exempt from disclosure
    under applicable law. If the reader of this message is not the intended
    recipient, or the employee or agent responsible for delivery of the
    message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please
    notify the sender immediately by e-mail and delete the material from any
    computer. Thank you.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupusers @
categoriesmyfaces
postedMay 18, '07 at 1:31p
activeMay 18, '07 at 7:15p
posts12
users4
websitemyfaces.apache.org

People

Translate

site design / logo © 2018 Grokbase