JSF 2.0 Accessing Application Scope bean from another Bean

前端 未结 3 654
鱼传尺愫
鱼传尺愫 2020-12-29 00:44

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 thi

3条回答
  •  离开以前
    2020-12-29 01:27

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

提交回复
热议问题