aspose file tools*
The moose likes JSF and the fly likes Accessing jsf bean from another jsf bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Accessing jsf bean from another jsf bean" Watch "Accessing jsf bean from another jsf bean" New topic
Author

Accessing jsf bean from another jsf bean

T Rai
Greenhorn

Joined: Apr 16, 2002
Posts: 20
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

Joined: Mar 29, 2008
Posts: 24
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


SCJP 1.5
Brajendra Mathema
Ranch Hand

Joined: Jun 02, 2009
Posts: 39
you can also access it using:

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

brajen
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Brajendra Mathema
Ranch Hand

Joined: Jun 02, 2009
Posts: 39
hi Tim, thanks for the idea
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing jsf bean from another jsf bean