• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing a managed bean from within another managed bean

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic