It's not a secret anymore!*
The moose likes JSF and the fly likes How to call function upon loading of JSF application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to call function upon loading of JSF application" Watch "How to call function upon loading of JSF application" New topic
Author

How to call function upon loading of JSF application

Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
IN JSP i used to put things in bean's constructor which is in APPLCIATION scope,
similarly
i want to load a function upon deploy of application where i can put .

i tried putting in listener but @ that time i am not getting faces config 's injection .

so is there any way out ?
Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
I would create a @ManagedBean with @ApplicationScope (or a @Named entity if using CDI/JSR-299) and put your logic in a method annotated with @PostConstruct

This will cause the @PostConstruct annotated method to be invoked immediately after the bean is constructed on JSF application start-up.


Lincoln Baxter, III | [url]http://ocpsoft.com/[/url] | Creator of: PrettyFaces - Bookmarks, Navigation, and Dynamic Parameters for JSF
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
Depending on what you need to do, you may be able to listen for the PostConstructApplicationEvent, otherwise what Lincoln suggested is probably your best bet.
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
here is my faces-config

here is my bean





*/



and my server log when i deploy this app

Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
My guess would be you're using the old version of faces-config (and possibly the old jars). The schema you're using for the faces-config is 1.2 change that to the 2.0 version, also double check the jars you're using are the 2.0 jars (look in the MANIFEST.MF file).
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46




i am getting...


Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
i need to do this in JSF 1.2 only..
is there any way out ?/

Thanks in advance ..

Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
You're not using 2.0 JSF jars:

INFO: Initializing Mojarra (1.2_10-b01-FCS) for context '/WebApplication14'

Are you using GlassFish v2? If you are there are a couple of options:
1: Upgrade to GlassFish v3
2: bundle the JSF 2.0 jars in your app and turn off class loading delegation and tell it you're bundling JSF in your sun-web.xml:

Probably have to change the dtd to use the correct one.
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
i need to do this in JSF 1.2 only..
is there any way out ?/

Thanks in advance ..
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
You've tried putting it in the constructor? You can always grab the FacesContext with FacesContext.getCurrentInstance. If that doesn't work for you, the next thing you've got is a PhaseListener.
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
i tried..
but at this time context is not initilized so i am not getting injected properties..


Thanks .
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
Try the PhaseListener, maybe someone else has a smarter idea, but with JSF 1.2 that's probably what you'll have to do.
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
nop its not working..
any one has any idea ?
Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
Try a ServletContextListener:

http://www.stardeveloper.com/articles/display.html?article=2001111901&page=1
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
yeah..
but i am not getting initilized context at the servletContextListener's context initilized method.
in jSF 1.2 beans having application scope gets initilized when it is needed not at start up //



Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
You may be able to construct a FacesContext manually if you require it... Or you should consider using a 3rd party Bean scope management system like Weld(CDI) that will provide access to beans at all phases of the Servlet lifecycle (and simplify your faces configuration):

1. http://ocpsoft.com/java/jsf-java/jsf-20-extension-development-accessing-facescontext-in-a-filter/
2. http://seamframework.org/Weld
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call function upon loading of JSF application
 
Similar Threads
Unfit for Fit
inserting a row when there is a sequence in that table
callflow glassfish V3
How to run a servlet application in subfolder of main application
checkbox values in session