0

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.

Sergio
  • 3,317
  • 5
  • 32
  • 51
gburgalum01
  • 181
  • 1
  • 15

1 Answers1

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.

noone
  • 19,520
  • 5
  • 61
  • 76
  • noone: Thanks for the response. I now understand why so many are using CODI or Seam to handle such issues. Upgrading to JSF 2.2 is not an option at this time, so I'm electing to leave the bean RequestScoped and add only those variables needed in session to a separate SessionScoped bean. Ideally, I'd prefer to include my functionality in a properly-scoped bean, but sometimes you have to make things work while staying within the bounds of your constraints. – gburgalum01 Oct 22 '13 at 12:04