Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 Accessing Application Scope bean from another Bean

Tags:

jsf-2

I am using jsf 2.0 and I have two bean Navigation (Application Scope ) and Module (Request Scope). I want to use methods of Navigation bean in Module Bean. I am doing in this way In Module Bean

 @ManagedProperty(value = "#{navigationBean}")
    private NavigationBean navigationBean;

But when I am trying to get navigationBean.SomeMethod it is not working as navigation bean is null . How to do this?

like image 976
Arvind Avatar asked May 23 '11 08:05

Arvind


1 Answers

The both beans needs to be a fullworthy @ManagedBean. The acceptor should have a public setter method for the injected bean. The injected bean is only available in @PostConstruct and beyond (i.e. in all normal event methods, but thus not in the constructor of the acceptor).

So, this ought to work:

@ManagedBean
@ApplicationScoped
public class Navigation {
    // ...
}

@ManagedBean
@RequestScoped
public class Module {

    @ManagedProperty(value="#{navigation}")
    private Navigation navigation;

    @PostConstruct
    public void init() {
        navigation.doSomething();
    }

    public void setNavigation(Navigation navigation) {
        this.navigation = navigation;
    }

}
like image 54
BalusC Avatar answered Oct 08 '22 21:10

BalusC