• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing a managedbean inside another managedbean  RSS feed

 
ihsan kocak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, how can i access a backingbean inside another backingbean?
 
Sachin Yewale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ihsan,
If your other managedBean is in session scope then you can use following code to access that bean in current backing bean as folows:

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inject it.

One of the fundamental concepts upon which JSF is based is Inversion of Control (IoC). In IoC, instead of being (gag!) "pro-active", you sit back and wait for good things to arrive to you.

More seriously, IoC is sort of the reverse of the Service Locator Design Pattern. One of the problems with the Service Locator is that it requires the referencing item to know things about the referenced item, including how (or at least when) it was created and how to locate it. That information has to be hard-coded into the referencing bean, which reduces flexibility.

In IoC, the referencing bean doesn't take an active role. Instead, the framework (in this particular case, JSF) is responsible for constructing the referenced bean (which is what makes it literally a "managed" bean) and for gluing the two beans together - a process known as injection. Injection is very simple. The referencing bean has a "set" property method that's used to store the referenced bean reference into the referencing bean, which is one of the signature characteristics of Plain Old Java Objects (POJOs).

And it's one of the reasons why so many JSF design issues can be solved without using any JSF-specific code at all. All of the JSF-specific code is in JSF itself.

JSF isn't the only IoC-based framework around by any means. One particularly popular IoC system is the Spring Framework, which has its own forum on the JavaRanch. Spring + JSF are an especially powerful team.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!