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
> >
>