JSF 2.0从另一个Bean访问Application Scope Bean [英] JSF 2.0 Accessing Application Scope bean from another Bean
问题描述
我正在使用jsf 2.0,并且有两个bean导航(Application Scope)和模块(Request Scope).我想在Module Bean中使用Navigation Bean的方法.我就是这样 在模块Bean中
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;
但是当我尝试获取navigationBean.SomeMethod
时,它不能正常工作,因为导航bean为null.该怎么做?
But when I am trying to get navigationBean.SomeMethod
it is not working as navigation bean is null . How to do this?
推荐答案
两个bean都必须是完整的@ManagedBean
.接受者应该对注入的bean有一个公共的setter方法.注入的Bean仅在@PostConstruct
及更高版本中可用(即,在所有正常事件方法中均可用,因此在接受器的构造函数中不).
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).
所以,这应该起作用:
@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;
}
}
这篇关于JSF 2.0从另一个Bean访问Application Scope Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!