问题描述
我已经看到很多关于将EJB注入到ViewScoped JSF托管bean中的讨论。这样做似乎是可以接受的做法。
I've seen quite a bit of discussion on the injection of EJBs into ViewScoped JSF-managed beans. It seems acceptable practice to do so.
我正在尝试将我的应用程序中的一个bean从RequestScoped更改为ViewScoped,以添加一些额外的所需功能。
I'm attempting to change one of the beans in my application from RequestScoped to ViewScoped to add some additional needed functionality.
我修改了EJB类以确保所有内容都是可序列化的。创建我的JSF bean时,最初可以访问EJB。但是,当用户操作触发尝试调用EJB方法的bean中的方法时,将抛出NullPointerException。
I've modified the EJB classes to ensure that everything is serializable. When my JSF bean is created, the EJBs are initially accessible. However, when a user action fires a method in the bean that tries to invoke an EJB method, a NullPointerException is thrown.
我从ViewScoped切换回来的bean对于RequestScoped,一切正常。
As soon as I switch the bean from ViewScoped back to RequestScoped, everything works fine.
这可能是正在使用的JSF实现中的错误吗?此应用程序使用MyFaces 2.1.12并在Websphere 7.0上运行。
Could this be a fault in the implementation of JSF being used? This application is using MyFaces 2.1.12 and runs on Websphere 7.0.
谢谢。
推荐答案
作为一个框架,CDI本身对Views没有任何了解。因此,注入 @ViewScoped
bean将无效。
CDI as a framework on its own doesn't know anything about Views. Thus injecting in a @ViewScoped
bean won't work.
这是使用JSF的主要缺点之一和CDI在一起。但你不是第一个遇到这个问题的人。
This is one of the major disadvantages of using JSF and CDI together. But you are not the first one to encounter this problem.
如果你坚持使用JSF 2.1实现,那么像或将以某种方式扩展您的CDI,因此您也可以 @Inject
in @ViewScoped
beans。
If you are stuck with JSF 2.1 Implementations, frameworks like Apache CODI or Seam 3 will extend your CDI in a way so you can also @Inject
in @ViewScoped
beans.
如果你能够升级到JSF 2.2(我建议你这样做),这个CDI扩展将是JSF实现的原生部分,你可以不加考虑地使用它们。请参阅。
If you are able to upgrade to JSF 2.2 (which I would recommended you to do), this CDI extension will be a native part of the JSF implementation and you are able to use both together without further ado. See this explanation.
这篇关于在ViewScoped JSF Bean中注入的EJB引用丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!