title: New @ViewScoped bean created for each h:form
body: <p>I have a problem with <code>@ViewScoped</code> beans created multiple times for different forms on the page.</p>
  
  <p>I created a simple test page:</p>
  
  
  
  <pre class="lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &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:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"&gt;
  &lt;h:head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h:form id="f1"&gt;
          &lt;h:commandButton value="action 1" action="#{testController.action}"&gt;
              &lt;f:ajax /&gt;
          &lt;/h:commandButton&gt;
      &lt;/h:form&gt;
      &lt;h:form id="f2"&gt;
          &lt;h:commandButton value="action 2" action="#{testController.action}"&gt;
              &lt;f:ajax /&gt;
          &lt;/h:commandButton&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and a backing bean:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@ManagedBean
  @ViewScoped
  public class TestController implements Serializable {
      private static final long serialVersionUID = 1L;
  
      public TestController() {
          System.out.println("TestController created");
      }
  
      public String action() {
          System.out.println("action() invoked");
          return null;
      }
  
      private void writeObject(ObjectOutputStream out) throws IOException {
          System.out.println("serialization");
          out.defaultWriteObject();
      }
  
      private void readObject(ObjectInputStream in) throws IOException,
              ClassNotFoundException {
          System.out.println("deserialization");
          in.defaultReadObject();
      }
  }
  </code></pre>
  
  <p>And here is web.xml:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0"&gt;
      &lt;display-name&gt;WebTest&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.xhtml&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.FACELETS_REFRESH_PERIOD&lt;/param-name&gt;
          &lt;param-value&gt;1&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt;
          &lt;param-value&gt;resources.application&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
          &lt;param-value&gt;client&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>When I click the <code>action 1</code> button, a new <code>TestController</code> bean is created and then serialized with <code>writeObject</code> method. Then, when I click <code>action 2</code> button, I expect that it will be deserialized, but instead, a new bean is created and those two beans act independently for each <code>&lt;h:commandButton&gt;</code>.</p>
  
  <p>I encoutered this problem with both Mojarra and Myfaces, so I guess it's not a bug in the library.</p>
  
  <p>Maybe I did some dumb mistake or missed something, but shouldn't <code>@ViewScoped</code> bean be the only bean for current view regardless of the number of <code>&lt;h:form&gt;</code> on the page?</p>
  
  <p><strong>UPDATE:</strong> I found out that the problem disappears when removing <code>&lt;f:ajax&gt;</code> tags; unfortunately, this is not the option for me.</p>
  
  <p><strong>UPDATE 2:</strong> I think I found the cause of the problem: when using multiple forms, only request form's <code>ViewState</code> is automatically updated. In my example, if I send request using the first form, the second form's view state is not updated unless I add <code>render=":f2"</code> to <code>&lt;f:ajax&gt;</code> in the first form. And since <code>@ViewScoped</code> bean is stored in the view state, the second form's view state will not include this bean and therefore a new bean will be created when sending request from the second form. So if my reasoning is true, I must manually update all forms in the view using <code>render</code> attribute to keep view states identical across forms. But this seems inconvenient especially if there are a lot of forms on the page. Aren't there any other solutions?</p>