1

I created this simple CDI bean:

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;


    @Named("DashboardController")
    @ViewScoped
    public class Dashboard implements Serializable
    {
    .......
    }

I removed all configuration from faces-config.xml. I created this beans.xml file into WEB-INF directory:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

When I opened the JSF page the bean cannot be found. Can you tell me what am I missing? I don't want to declare the beans into faces-config.xml.

P.S I don't know if this is important or not but this is a WAB package with CDI beans.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

2

You'll need to use ViewAccessScoped instead of ViewScoped.

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
//Note the different import
import javax.faces.context.FacesContext;
import javax.inject.Named;


    @Named("dashboardController")
    @ViewAccessScoped
    public class Dashboard implements Serializable
    {
    .......
    }

You should also start the name in Named with a non-capital letter.

Tycho91
  • 53
  • 3
  • 1
    Yes, but as far as I know `ViewAccessScoped` is from the CODI framework not CDI. – Peter Penzov Mar 14 '13 at 12:08
  • 2
    OP is not using CODI, and you haven't even mentioned the framework in your answer. As far as any other onlooker to this question is concerned, it appears as if `ViewAccessScoped` is part of CDI – kolossus Mar 15 '13 at 05:43
0

You might need to add the faces_config file to your META-INF folder of your WAB as described in this thread

That aside, even if the bean is found, you might still have problems with the scoping; You can't apply a JSF scope to a CDI bean. CDI's @ConversationScoped is a somewhat less than convenient alternative to JSF's @ViewScoped. The inconvenience of the scope lies in the fact that you need to inject an extra managed object and you have to actively manage the scope yourself. To use:

  1. Annotate your bean with @ConversationScoped

    @Named("DashboardController")
    @ConversationScoped
    public class Dashboard implements Serializable
     {
    
     }
    
  2. Inject the Conversation object into your bean

    @Inject
    private Conversation conversation;
    
  3. On this object, you need to call begin() and end() to start the "conversation" (a la viewscope) and "end" the conversation (like JSF does by destroying a viewscoped bean) respectively. This is a matter of design and context. At the very least, you can call conversation.begin() in a @PostConstructor. Where you end the conversation depends on your specific use case

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Thank you for the information. Can I automatically free the resources using this code: `@Remove public void finishIt(){ conversation.end(); }`. I want to place it at the bottom of the managed bean and let the Application server to manage the resources alone when the user closes the page. – Peter Penzov Mar 15 '13 at 07:58
  • @PeterPenzov yes you can. In an EJB, that's the perfect place to place it – kolossus Mar 15 '13 at 13:18
  • As @BalusC recommended here: http://stackoverflow.com/questions/15427217/ajax-pages-with-cdi-beans-and-conversationscoped The new JSF 2.2 will fix this problem. I just will switch to the new version. – Peter Penzov Mar 15 '13 at 13:21
  • @PeterPenzov that's fine. Don't think 2.2. has been officially released though. – kolossus Mar 15 '13 at 13:35
  • link is broken my friend – Ebuzer Taha KANAT Aug 20 '17 at 13:04