aspose file tools*
The moose likes JSF and the fly likes Injecting Beans as Parameters (Injected Bean reference is null) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Injecting Beans as Parameters (Injected Bean reference is null)" Watch "Injecting Beans as Parameters (Injected Bean reference is null)" New topic
Author

Injecting Beans as Parameters (Injected Bean reference is null)

V. Rentschler
Greenhorn

Joined: Feb 23, 2009
Posts: 5
Hi, I am getting into JSF.. or rather not. I am trying to write a simple Application, but I just cannot get it to work. If anyone looks over this, I thank you for your time in advance!
I deploy on Jetty and my JSF-Engine is MyFaces, though I doubt that it matters.

I try to write a simple "Guestbook" application which allows an own guestbook for every user, which only he can access.
I managed to write a "NewEntry.jsp", and it works fine.

Classes I wrote:
Guestbook - Guestbook Entity, contains a Vector of...
GuestbookEntry - GuestbookEntry Entity, contains all the data of an entry as strings. I also used it as Backing Bean for adding a new Entry, dunno if that's good.. (but it worked)
User - A User (contains UserID and the username etc.)
GuestbookSvc - A managed application scope bean. So it should be Singleton..(?) manages login, holds user passwords, user guestbooks, etc.
LoginBean - A managed session scope bean. Bascially the backing bean for my login site + it should store if the user is logged in obviously

In the GuestbookSvc constructor I create some dummy data.

Now, as you can see I need the LoginBean to access the instance of GuestbookSvc. As I have no factory class, I inject it over a parameter.
Let me show you the managed beans in my faces-config.xml:


The GuestbookSvc is created succesfully, so there is no problem in that area.
Here is the code of my LoginBean:


My login-site (guestbook.jsp) looks like this:

(As you can see I actually access the guestbooksvc there, to retrieve my dummy users...)

Now, my looger says: LoginSvc: gbsvc is null. Anyone has a clue why gbsvc *is* null?
Thanks in advance,

sincerly yours
V. Rentschler
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Because managed property injection only happens after construction.

JSF cannot do loginBean.setGbsvc(gbsvc) without doing LoginBean loginBean = new LoginBean() first.

Edit: if you actually want to do some logic after construction AND managed property injection, then use a method which you annotate with @PostConstruct. This method will be invoked after managed property injection.

E.g.

This is possible since Java EE 5 / JSF 1.2 only though.
V. Rentschler
Greenhorn

Joined: Feb 23, 2009
Posts: 5
Bauke Scholtz wrote:JSF cannot do loginBean.setGbsvc(gbsvc) without doing LoginBean loginBean = new LoginBean() first.


Oh my... it's pretty simple, isn't it? No magic happening...

Thank's alot!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Injecting Beans as Parameters (Injected Bean reference is null)
 
Similar Threads
Could not navigate from one page to another !!!
managed bean in JSF.
problem JSF - datatable from DB
relation between backing beans
The requested resource (Servlet Faces Servlet is not available) is not available.