5

I have a problem with JSF, CDI project. I did a lot of research and I found that in CDI there is no @ViewedScoped annotation. I solving problem with ajax based page with dialog. I want to pass variable to dialog from datatable. For this purpose, I can't use @RequestedScoped bean because value is discard after end of request. Can anyone help me to solve it? I can't use @SessionScoped but it's a bad practice IMHO. Or maybe save only this one variable into session who knows. Can you guys give me any hints how to solve this problem elegantly?

import javax.enterprise.context.ApplicationScoped;    
@ApplicationScoped
public class ServiceBean implements Serializable {
...
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class SomeBean {

@Inject
ServiceBean serviceBean;


@Postconstruct ...

Here is the error message:

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean warDetailBean
Tarik
  • 4,961
  • 3
  • 36
  • 67
Milkmaid
  • 1,659
  • 4
  • 26
  • 39
  • Use either CDI beans or JSF beans. Do not unnecessarily think of mixing them, – Tiny Feb 14 '15 at 14:50
  • 1
    "*I do a lot of research and I found that in CDI is not `@ViewedScoped` annotation.*" This was true only before Java EE 7. Java EE 7 (JSF 2.2) transparently supports a compatible view scope (from `javax.faces.view.ViewScoped`) in flavour of the JSF view scope (from `javax.faces.bean.ViewScoped`). – Tiny Feb 14 '15 at 14:58

1 Answers1

15

First, If you are attempting to use CDI, you need to activate it by putting a WEB-INF/beans.xml file in your application (note that this file can be empty), more informations about that file could be found in the Weld - JSR-299 Reference Implementation.

As you are using Tomcat, please be sure to respect all the configuration requirements by following the steps in How to install CDI in Tomcat?

Second, Even if you can use @Inject inside a JSF managed bean, It's preferable that you don't mix JSF managed beans and CDI, please see BalusC's detailed answer regarding Viewscoped JSF and CDI bean.

So if you want to work only with CDI @Named beans, you can use OmniFaces own CDI compatible @ViewScoped:

import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;

@Named
@ViewScoped
public class SomeBean implements Serializable {

    @Inject
    ServiceBean serviceBean;
}

Or, if you want to work only with JSF managed beans, you can use @ManagedProperty to inject properties:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class SomeBean{

@ManagedProperty(value = "#{serviceBean}")
ServiceBean serviceBean;

}

See also:

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • I ll give the OmniFaces one more chance cause when I add dependency to it, it generate errors. CDI is setup properly everything work fine. I just need @ViewScoped bean. Cause I have a lot of ajax on page. – Milkmaid Feb 14 '15 at 15:19
  • @Milkmaid please see the showcase i think that may help you: http://showcase.omnifaces.org/cdi/ViewScoped – Tarik Feb 14 '15 at 15:20
  • OmniFaces said that I dont have CDI. But I have it in dependency with weld implementation. Do I need Application server cause I user Tomcat – Milkmaid Feb 14 '15 at 15:23
  • did you added the WEB-INF/beans.xml file to your application? – Tarik Feb 14 '15 at 15:25
  • Yes it is there all the time. – Milkmaid Feb 14 '15 at 15:25
  • please follow how to configure CDI using tomcat from here: http://balusc.blogspot.com/2013/10/how-to-install-cdi-in-tomcat.html – Tarik Feb 14 '15 at 15:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70920/discussion-between-milkmaid-and-tarik). – Milkmaid Feb 14 '15 at 15:34
  • 4
    In JavaEE7 containers a `beans.xml` file is not necessary as CDI is active by default in EE7 – NBW Feb 16 '15 at 14:45
  • @NBW True, but I said that he must add beans.xml because he is using Tomcat. Anyway, thanks for your exactness:) – Tarik Feb 16 '15 at 16:46