Get a managed bean link in another managed bean

I am trying to get a managed bean instance in another managed bean thanks to this BalusC page : here

Using the findBean method, I get my bean, but with ManagedProperty I can not get my bean.

My bean for input:

 @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 works, but:

 @ManagedProperty("#{locale}") // OR localeBean, LocaleBean... private LocaleBean locale; 

does not work...

com.sun.faces.mgbean.ManagedBeanCreationException: Cannot use bean géré "login". Les problèmes suivants ont été détectés: - La propriété "locale" du bean géré "enter" nexiste pas.

Why?

+4
source share
3 answers

you should write getter / setter for a bean that annotates @ManagedProperty

+3
source

I see that your LocaleBean is associated with a session. Instead of annotating @ManagedProperty and getters / setters, you can refer to a managed bean with a limited communication session directly from the code using the getSessionMap method of the servlet context:

 LocaleBean locale = (LocaleBean) FacesContext.getCurrentInstance() .getExternalContext().getSessionMap().get("locale"); 
+2
source

If you want to call LocaleBean in your Login bean, you must run SessionScoped or higher.

If you want to call the Locale bean in the Login bean, you need to do

 @ManagedProperty("#{locale}") private LocaleBean locale; 

Call the appropriate set of e for this. You don't need it

 LocaleBean locale = findBean("locale"); 

Now you can use the locale, since it is local in your Login Bean:

 String s = local.getLocale(); 
0
source

All Articles