5

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 ?

Community
  • 1
  • 1
Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • is it because my `locale` bean may have not bean instantiated before `login` bean...? – Olivier J. Dec 27 '12 at 15:15
  • 1
    I don't understand French very well but did you created getters and setters for this property? – partlov Dec 27 '12 at 15:15
  • 1
    I just saw this post : http://stackoverflow.com/questions/5165567/how-to-inject-entire-managed-bean-via-managedproperty-annotation. It's a getter/setter problem indeed. Thank you partlov – Olivier J. Dec 27 '12 at 15:21
  • It'd be helpful for yourself (and others when posting it as a question!) if you reconfigure your environment to use English locale. If you search/google/share English error messages, you'll get more hits and quicker clues. – BalusC Dec 27 '12 at 15:23
  • I will do that next time sorry. Thank you for your previous mentionned post BalusC – Olivier J. Dec 27 '12 at 15:43

3 Answers3

4

you should write getter/setter for bean which is annotated @ManagedProperty

2

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");
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
0

If u want to call LocaleBean in your Login bean needs to be SessionScoped or above.

If you want to call Locale Bean on Login Bean u have to do

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

Call the appropriate set e get for that. You don't need this

LocaleBean locale = findBean("locale");

You can use now locale as it is local like this in your Login Bean:

String s = local.getLocale(); 
Nuno
  • 11
  • 2