I try to get instance of managed bean in another managed bean thanks to this BalusC post : here
With findBean
method, it's great, I retrieve my bean but with ManagedProperty
I can not get my bean.
My bean to inject is this one :
@ManagedBean(name="locale")
@SessionScoped
public class LocaleBean {
private String locale;
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(locale));
this.locale = locale;
}
}
So when I call LocaleBean locale = findBean("locale");
in my login
bean it's work but :
@ManagedProperty("#{locale}") // OR localeBean, LocaleBean...
private LocaleBean locale;
doesn't work...
com.sun.faces.mgbean.ManagedBeanCreationException: Impossible de créer le bean géré «login». Les problèmes suivants ont été détectés : - La propriété «locale» du bean géré «login» n’existe pas.
Why please ?
you should write getter/setter for bean which is annotated @ManagedProperty
I see that your LocaleBean is session scoped. Instead of the @ManagedProperty annotation and the getters/setters, you can reference another session scoped managed bean directly from the code using the getSessionMap method of the servlet context:
LocaleBean locale = (LocaleBean) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("locale");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With