Thank you for the tip - it helped me. The problem was that the Bean did not
exist yet. And yes - it also did not need to have the capital S.
If i now go to a page which uses the sessionBean it will get created and
then I can use the findBean Method inside another Bean.
Is there a way to create a Bean Instance programmatically?
In JSF 1.2 we used this Method:

/**
  * Gibt die BackingBean mit dem Namen pName zurück.
  * Ist die BackingBean noch nicht vorhanden, wird die Bean erzeugt.
  *
  * @param pString
  * @return Object
  */
public static Object getBackingBean( String pName ) {
  ELContext elContext = FacesContext.getCurrentInstance().getELContext();
  Object ret = elContext.getELResolver().getValue(elContext, null, pName);
  return ret;
}

It would create an instance if it wasn't there before. Is there something
similar in JSF2 I can add to the findBean method so that it creates an
instance instead of returning null?

Toby

On Fri, Aug 19, 2011 at 2:08 PM, Rudy De Busscher <[email protected]>wrote:

> Maybe stupid thing, but have you checked the case of your bean name (is it
> "sessionBean" or "SessionBean" or ...)
>
> If the bean is supposed to be on the session, you can have access (to
> verify
> if the bean exists) to it with the command
>
> FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
>
> regards
> Rudy
>
> On 19 August 2011 12:49, Tobias Eisentrager <[email protected]
> >wrote:
>
> > I tried Jakob's suggestion but it did not work - it returns null.
> >
> > Are there any other ways to access the current SessionBeans?
> >
> > Or, maybe the design of the App is not good - maybe i'm doing something
> > wrong.
> >
> > Here is what i want to do:
> >
> > I have a Class which I use for all Server communication called
> > DataConnector. It is a private member of my main Session Bean, called
> > SessionBean. So, when I need Data i want to fire a command like this:
> >
> > Code in some ManagedBean:
> >
> > List<Item> items = null;
> >
> > public List<Item> getItems() {
> >    if(items==null) {
> >        SessionBean sessionB = (SessionBean)
> > JSF2Util.findBean("SessionBean");
> >        List<Item> items = sessionB.getDataConnector().cmdGetItems();
> >    }
> >    return items;
> > }
> >
> > JSF2Util:
> >
> > @SuppressWarnings("unchecked")
> > public static <T> T findBean(String beanName) {
> > FacesContext context = FacesContext.getCurrentInstance();
> > return (T) context.getApplication().evaluateExpressionGet(context, "#{" +
> > beanName + "}", Object.class);
> > }
> >
> > Since the findBean does not work for some reason, should I do this
> scenario
> > different?
> >
> > Oh, all this used to work in JSF 1.2 in another similar app of mine.
> >
> > Thanks
> >
> > Toby
> >
> >
> >
> > On Mon, Jun 27, 2011 at 4:41 AM, Leonardo Uribe <[email protected]>
> wrote:
> >
> > > Hi
> > >
> > > I have been thinking about this one, and maybe there is a
> > > misunderstanding with @ManagedProperty annotation. That annotation is
> > > not servlet api, is in JSF or in java EE (i don't know which subset
> > > are you using). If is the JSF variant, note that annotation only works
> > > for managed beans, so you can't use it on other objects that are not
> > > instantiated by JSF.
> > >
> > > The suggestion of Jakob should work.
> > >
> > > regards,
> > >
> > > Leonardo Uribe
> > >
> > > 2011/6/26 Jakob Korherr <[email protected]>:
> > > > Hi,
> > > >
> > > > Try using this:
> > > >
> > > > FacesContext facesContext = FacesContext.getCurrentInstance();
> > > > YourBean yourBean =
> > > > facesContext.getApplication().evaluateExpressionGet(facesContext,
> > > > "#{yourBean}", YourBean.class);
> > > >
> > > > Regards,
> > > > Jakob
> > > >
> > > > 2011/6/26 Tobias Eisentrager <[email protected]>:
> > > >> Hello Group - this is my private email address.
> > > >>
> > > >> I have already asked this on
> > > >>
> > >
> >
> http://stackoverflow.com/questions/6466783/access-another-managed-bean-in-jsf2-1-with-servlet-2-4
> > > >> and have not gotten a sufficient answer yet.
> > > >>
> > > >> We have recently upgraded from JSF 1.2 to 2.1. We are running on
> > > >> WebSphere 6.1 which has Servlet 2.4
> > > >>
> > > >> We are using the following libraries: myfaces 2.1.1 el-api-2.2
> > > >>
> > > >> Now the only problem we have is that we cannot access the other
> > > >> Backing Beans like we did before with:
> > > >>
> > > >> public static Object getBackingBean( String pName ) {
> > > >>   ELContext elContext =
> > > FacesContext.getCurrentInstance().getELContext();
> > > >>   Object ret = elContext.getELResolver().getValue(elContext, null,
> > > pName);
> > > >>   return ret;
> > > >> }
> > > >>
> > > >> This will always return null. We have also tried:
> > > >>
> > > >> Beanclass bean = (Beanclass)
> > > FacesContext.getCurrentInstance().getApplication()
> > > >> .getELResolver().getValue(elContext, null, "beanclass");
> > > >>
> > > >> which return null as well.
> > > >>
> > > >> We have tried the @ManagedProperty annotation but this is apparently
> a
> > > >> Servlet 2.5 feature. Is it possible that the ELContext uses DI now
> by
> > > >> default? Is there a way to get an Instance of another backing Bean
> in
> > > >> JSF2.1 and Servlet 2.4?
> > > >>
> > > >> Thanks!
> > > >>
> > > >> Toby
> > > >>
> > > >
> > > >
> > > >
> > > > --
> > > > Jakob Korherr
> > > >
> > > > blog: http://www.jakobk.com
> > > > twitter: http://twitter.com/jakobkorherr
> > > > work: http://www.irian.at
> > > >
> > >
> >
>
>
>
> --
> Rudy De Busscher
> http://www.c4j.be
>

Reply via email to