This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing jsf bean from another jsf bean  RSS feed

 
T Rai
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
Srini Mutpur
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Brajendra Mathema
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also access it using:

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

brajen
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi Tim, thanks for the idea
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!