title: Using CDI (Context & Dependency Injection) backing beans instead of Managed Beans
body: <p>I rode that is recommended to use CDI beans as backing beans instead of JSF managed beans.</p>
  
  <p>So i decided to create a little example, to understand how it works, for a <strong>@RequestScopedBean</strong>:</p>
  
  <p>-instead of using <strong>@ManagedBean("beanName")</strong> ,i use <strong>@Named("beanName")</strong> </p>
  
  <p>-instead of using <strong>javax.faces.bean.RequestScopped</strong> i use <strong>javax.enterprise.context.RequestScoped;</strong></p>
  
  <p>The demo program is very simple, i have a field and a submit button, when the user inputs something and the page is refreshed, the inputed value is not displayed anymore(It last while the request lasts right?). I think i did everything ok, but i get an exception that says:</p>
  
  <blockquote>
    <p>WARNING: StandardWrapperValve[Faces
    Servlet]: PWC1406: Servlet.service()
    for servlet Faces Servlet threw
    exception
    javax.el.PropertyNotFoundException:
    /index.xhtml @19,47
    value="#{cdiBean.passedValue}": Target
    Unreachable, identifier 'cdiBean'
    resolved to null</p>
  </blockquote>
  
  <p>This is how my program looks like:</p>
  
  <p><strong>index.xhtml</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
  
  &lt;h:head&gt;
          &lt;title&gt;RequestScope demo CDI(Component Dependency Injection)&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
  
      &lt;h:form&gt;
  
      &lt;h3&gt;RequestScope demo CDI(Component Dependency Injection)&lt;/h3&gt;
  
      &lt;h:inputText value="#{cdiBean.passedValue}"/&gt;
      &lt;br/&gt;
      &lt;h:commandButton value="submit" action="index"/&gt;
  
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>DemoBB.java</strong></p>
  
  <pre><code>package backingbeans;
  
  import javax.enterprise.context.RequestScoped;
  import javax.inject.Named;
  
  @Named("cdiBean")//The Named anotation indicates that this is a CDI bean
  @RequestScoped//If we use CDI beans the @RequestScoped annotation must come from: javax.enterprise.context.RequestScoped;
  public class DemoBB {
  
      //This value will be saved on the session only until the server responds to the request
      private String passedValue;
  
      public String getPassedValue() {
          return passedValue;
      }
  
      public void setPassedValue(String passedValue) {
          this.passedValue = passedValue;
      }   
  }
  </code></pre>
  
  <p>-Where is my mistake?</p>
  
  <p>-What is the advantage of using this approach? I still don't understand that.</p>