On 1/4/06, Garner, Shawn <[EMAIL PROTECTED]> wrote:
>
> How do you get access to one managed bean from within another managed
> bean?
>
> We have some business logic that depends on values in another managed.


If you're trying to gain access from a class that extends AbstractFacesBean
or AbstractViewController, this is really simple:

    MyBean bean = (MyBean) getBean("name"); // "name" == managed bean name
of the other bean

If you are in a class that doesn't extend one of these, it's a little more
work but still straightforward:

  FacesContext context = FacesContext.getCurrentInstance();
  ValueBinding vb = context.getApplication().createValueBinding("#{name}");
  MyBean bean = (MyBean) vb.getValue(context);

Either of the above techniques will cause the other managed bean to be
created, if it doesn't exist.  If you *know* it exists, and what scope it is
in, you can also use the appropriate scoped map.  Assume the other bean is
in session scope:

  FacesContext context = FacesContext.getCurrentInstance();
  MyBean bean = (MyBean) context.getExternalContext
().getSessionMap().get("name");

Shawn


Craig

Reply via email to