• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a managed bean from within another managed bean

 
Mike Litherland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just starting out using JSF and I'm a bit confused. I'm writing a web app that has very complex user roles and everything you do within the app needs to be determined by your user role. As a result all of my commandButtons and commandLinks point to methods of my managed user bean which determines whether the user can perform a given action.

So far so good. Now I want my user to fill in a form that's backed by a different bean (in this case to add a new user). Only problem is I can't see how to get access to the different bean (let's call it the account bean) from within the user bean. I've looked at getting an instance of the FacesContext and played with the lifecycle stuff but I'm still stumped.

So here's my two questions - am I thinking about this the right way? And, how do I access my account bean within my user bean?

Thanks so much,
Mike
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done some role-sensitive stuff in JSF, but it was simple enough not to consider farming out to a user properties class:



If you do want to centralize user info into a bean that logic bean(s) reference, the way to go is IoC. Both beans are managed beans, the logic bean is given session scope and the user info bean (also in session scope) is injected into the logic bean using the wiring functionality that's in the faces-config.xml file.

Here's some functionally-equivalent wiring, although instead of user properties, the injected bean contains captured search criteria:
 
Mike Litherland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. I especially found the hint about the managed property useful, Tim. It's exactly what I was looking for. I may just get this JSF stuff yet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!