• 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 jsf bean from another jsf bean

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I have a jsf bean with session scope that I need to access from another bean.
For example:



In a method in some other class, I need this User bean.
What's approach/How can I get the User bean from the session that I can use the value stored in it originally?


Thanks.
TR
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the "User" manage property for the bean which wants to use the user bean. Say person bean wants to use the user bean
<managed-bean>
<managed-bean-name>Person</managed-bean-name>
<managed-bean-class>com.abc.domain.Person</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>user</property-name>
<value>#{User}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>User</managed-bean-name>
<managed-bean-class>com.abc.domain.User</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean> >
and also create the getter and setter for user in the person bean.
Person(){
com.abc.domain.User user;
// getter/setter for user
}
Note: user property name in the person bean and the managed property name should match.

Thanks
Srini
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also access it using:

MangedBeanClass mb = (ManagedBeanClass)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("managed bean instance");

brajen
 
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

brajen mathema wrote:you can also access it using:

MangedBeanClass mb = (ManagedBeanClass)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("managed bean instance");

brajen



But you shouldn't. It's ugly, non-portable and violates the principle of Inversion of Control. Much cleaner and more flexible to link beans together by wiring them in the framework instead of hard-coding their connection with platform-specific functions.
 
Brajendra Mathema
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tim, thanks for the idea
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic