Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 23426
159
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
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic