• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

access another bean information

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess that is correct.. if that method is having a public scope.. it shd technically work..
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If both are managed beans, just inject the one in the other as a managed property in faces-config.xml.
 
Saloon Keeper
Posts: 28717
211
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
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28717
211
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
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic