Injected EJB Reference Lost in ViewScoped JSF Bean

帅比萌擦擦* 提交于 2019-12-13 04:46:09

问题


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.

I'm attempting to change one of the beans in my application from RequestScoped to ViewScoped to add some additional needed functionality.

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.

As soon as I switch the bean from ViewScoped back to RequestScoped, everything works fine.

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.

Thanks.


回答1:


CDI as a framework on its own doesn't know anything about Views. Thus injecting in a @ViewScoped bean won't work.

This is one of the major disadvantages of using JSF and CDI together. But you are not the first one to encounter this problem.

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.

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.



来源:https://stackoverflow.com/questions/19454322/injected-ejb-reference-lost-in-viewscoped-jsf-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!