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

access another bean information

lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
I have created 2 managed bean, dummyBean and InfoBean. In my InfoBean, i need to get values from dummyBean, how do i do that?

i uses this method:
dummyBean db= new dummyBean();
string name=db.getName();

is this correct? or it shld be done in another way/method?

thanks.
sriram vemaraju
Greenhorn

Joined: Feb 11, 2008
Posts: 16
i guess that is correct.. if that method is having a public scope.. it shd technically work..
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
well in my opinion it is correct.... but it will create a problem when you try to put the values from the jsp page. because it willl overrite the dummy bean values....

so try to avoid it... you are in other words changing the life cycle of JSF.

keep it simple.

Regads


i am Java +ve Now !
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
If both are managed beans, just inject the one in the other as a managed property in faces-config.xml.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

JSF is an Inversion of Control (IoC) framework. To access one managed bean from another managed bean, you simply setup up the faces-config file to tell it to inject it as a managed-property. And code a setter for the second bean in the first bean.

For example:



This bean allows me to test off-line by keeping the DAO and JSF details out of the business program logic. The userService bean is injected into the securityContext bean so that the securityContext can get information about the user's account.


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

Joined: Oct 08, 2006
Posts: 2458
sriram vemaraju wrote:i guess that is correct.. if that method is having a public scope.. it shd technically work..
The posted code doesn't use the managed bean instance, but it creates a brand new bean instance which isn't managed by JSF at all.
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
Tim Holloway wrote:JSF is an Inversion of Control (IoC) framework. To access one managed bean from another managed bean, you simply setup up the faces-config file to tell it to inject it as a managed-property. And code a setter for the second bean in the first bean.


Do you mean in my InfoBean i need to have something like this:

private dummyBean db;

public setdb(dummyBean db){

}

what will be the code in the setter??


if i do the above code, for the faces-config is it something like this:
<managed-property>
<description>Spring-injected persistence service</description>
<property-name>dummyBean </property-name>
<value>#{db}</value>
</managed-property>


am i on the right track?


After setting all this, i can just call the values by "db.getxx()"?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

You don't need to modify anything in dummyBean - that's one of the benefits of IoC. The JSF framework will construct the dummyBean (since you said it's a managed bean) and use the target bean's setter to inject the dummyBean into the target.

And yes, if you want the target bean code to access properties in dummyBean, the "db.getXXX()" is all you need.

About the only thing I can add is that best practice would be to make the dummy bean classname be "DummyBean" (starting with upper-case) and that the managed instance of DummyBean would typically be named "dummyBean" in the faces-config.xml file. Oh, and you have tour property name and value settings backwards. It should be:



Which would refer you back to something like this:


You don't need the "id=" attribute, but it helps me find them in my IDE.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access another bean information