File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How to access method of another Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to access method of another Bean" Watch "How to access method of another Bean" New topic

How to access method of another Bean

Mario Luef

Joined: Oct 05, 2011
Posts: 6

Hello Guyz!

I need to access the method of another bean and this method should use data from the calling bean.

Is this possible without creating a new instance of that method-bean?

Because im building a universal database-searchengine configured by xml and also the commandbuttons
are defined there. And i dont want that the guy who is using this engine has to change or add programm code!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what is the scope of that bean which contains the method? If the scope is session, then you can get the bean instance from session object.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410


I have you know we're all respectable professional professionals here. Just look at how dignified our Moose is!

JSF is based on the Inversion of Control concept. In IoC, the way for one bean to reference another bean is that the framework injects the referenced bean into the referencing bean using its POJO "set" method plus wiring rules which originally came from faces-config.xml but can (as of JSF2) also be constructed from @ManagedBean/@ManagedProperty annotations..

It sounds like you have a use case I've worked with a time or 2 myself. I present a search form, the user enters search criteria, hits the "search" button and results come back. Here's how I do it:

First, I have a backing bean for the search form. This is a short-lived bean and is one of the few cases in JSF where you can get away with using Request Scope.

The primary bean that will actually fire off the search and manage the search results is typically a session scope bean that has been wired into (injected) into the search form backing bean.

The action method of the search bean collects the search parameters, which are properties of the search bean and thus have been set by JSF when you clicked the "search" button. It can then invoke a search method on the search bean that was injected into the search form bean. It's up to the designer whether to inject the search properties as properties to the search bean or simply pass them as arguments to the search function (or mix-and-match, for that matter).

Once the search form's action processor has done its thing, it returns a navigation indicator that would either present the results of the search or some type of error page. Unless it would rather go somewhere else, instead.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: How to access method of another Bean
It's not a secret anymore!