title: JSF 2.0 Injecting managed bean with a different scope
body: <p>I have a controller that is stateless which takes care of processing forms. This is defined as <code>ApplicationScoped</code>. On my page I have a form associated to a backing bean defined as a  <code>ViewScoped</code>.</p>
  
  <p>The error I got when I want to process the form:</p>
  
  <pre><code>serverError: class com.sun.faces.mgbean.ManagedBeanCreationException Unable to create managed bean myController.  The following problems were found:
       - The scope of the object referenced by expression #{myFormBean}, view, is shorter than the referring managed beans (myController) scope of application
  </code></pre>
  
  <p>In my form:</p>
  
  <pre><code>       Name: &lt;h:inputText value="#{myFormBean.name}" id="name" /&gt;
          &lt;h:commandButton value="Save Name" action="#{myController.processForm}"&gt;
              &lt;f:ajax render="nameResult" /&gt;
          &lt;/h:commandButton&gt;
         Your name is &lt;h:outputText value="#{myFormBean.name}" id="nameResult"/&gt;
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>@ManagedBean
  @ApplicationScoped
  public class MyController {
      @ManagedProperty("#{myFormBean}")
      private MyFormBean myBean;
      public void processForm() {
          System.out.println(myBean.getName());
          // Save current name in session
          FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
                  "name", myBean.getName());
      }
  }
  </code></pre>
  
  <p>The backing bean:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class MyFormBean {
      private String name;
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>I could solve that by setting the controller as SessionScoped but it’s not a clean way since the controller is stateless, so I don’t need one controller for each session. One controller for the whole application should be sufficient.</p>
  
  <p>I have a Spring MVC background, that’s why I am confused on how to do things with JSF 2.0</p>