ManagedBean 访问另一个 ManagedBean
我可能非常接近解决方案,但我是 JSF 新手,我没有看到我的错误。 我有第一个 SessionScoped Managed Bean 代表业务信息(地址、网站等)
@Named(value = "businessController")
@SessionScoped
public class BusinessController implements Serializable {
private Business current;
@EJB private BusinessFacade ejbFacade;
....
我有第二个 SessionScoped Managed Bean 代表登录用户
@Named(value = "loginController")
@SessionScoped
public class LoginController implements Serializable {
private Login current;
@EJB
private LoginFacade ejbFacade;
@ManagedProperty(value="#{businessController}")
private BusinessController businessController;
public BusinessController getBusinessController() {
return businessController;
}
public void setBusinessController(BusinessController businessController) {
this.businessController = businessController;
}
当用户登录时,我从 loginController 设置当前属性 根据当前用户,我想从businessController 设置业务属性:
businessController.setCurrent(current.getBusiness());
我的问题是businessController 属性为null!
我使用 NetBeans 7.0.1 和 GlassFish 3.1 在调试模式下,我可以看到一个带有值的 viewId 变量
>No current context (stack frame)<
不幸的是它没有给我带来任何提示。
任何帮助将不胜感激 谢谢
I'm probably really close to the solution but I'm new with JSF and I don't see my mistake.
I have a first SessionScoped Managed Bean that represents Business information (address, website, ...)
@Named(value = "businessController")
@SessionScoped
public class BusinessController implements Serializable {
private Business current;
@EJB private BusinessFacade ejbFacade;
....
I have a second SessionScoped Managed Bean that represents the logged in user
@Named(value = "loginController")
@SessionScoped
public class LoginController implements Serializable {
private Login current;
@EJB
private LoginFacade ejbFacade;
@ManagedProperty(value="#{businessController}")
private BusinessController businessController;
public BusinessController getBusinessController() {
return businessController;
}
public void setBusinessController(BusinessController businessController) {
this.businessController = businessController;
}
When a user logs in, I set the current attribute from the loginController
Depending on this current user, I want to set the business attribute from the businessController :
businessController.setCurrent(current.getBusiness());
My problem is that the businessController attribute is null !
I use NetBeans 7.0.1 and GlassFish 3.1
In debug mode, I can see a viewId variable with the value
>No current context (stack frame)<
Unfortunately it doesn't ring any bell to me.
Any help would be appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将 JSF 托管 Bean 与 CDI 托管 Bean 混合在一起。
您的 BusinessController 使用 CDI 注释
@Named
进行注释,但注入了@ManagedProperty
注释(来自 JSF)。 CDI 托管 Bean 需要使用@Inject
进行注入。在这种情况下不需要 getter 或 setter。如果您倾向于使用 CDI,请确保导入正确的@SessionScoped
:尝试以下操作(在确保导入了正确的作用域类之后):
You are mixing JSF managed beans with CDI managed beans.
Your BusinessController is annotated with the CDI annotaion
@Named
but gets injected with the@ManagedProperty
annotation (from JSF). CDI managed beans need to be injected with@Inject
. No getter or setter needed in this case. If you tend to use CDI, make sure that you import the correct@SessionScoped
:Try the following (After making sure to have the correct scope class imported):