title: Spring MVC 3 Command Object values being lost between GET and POST operation
body: <p>My domain object does not keep the values that are not explicitly referenced in the JSP file between a GET and a POST operation on the same controller.  Here is a sample with error checking omitted</p>
  
  <p>I have a domain object.</p>
  
  <pre><code>class foo
  {
      private int fieldA;
      private String fieldB;
      // .. getters and setters omitted
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@Controller
  public class MyController
  {
  @Autowired
  private IDaoService daoService;
  
  @RequestMapping(value = "/display", method = RequestMethod.GET)
  public String newForm(@RequestParam("id") Long id, Model model, Principal principal) throws Exception
  {
      // check that the user has permissions
      ...
  
      // get the object
      Foo foo = daoService.read(id);
      // at this point foo.fieldA is equal to the input id
      model.addAttribute("foo", foo);
  
      // return the JSP path
  }
  
  @RequestMapping(value="/update", method = RequestMethod.POST)
  public String update(@ModelAttribute("foo") Foo foo,
          BindingResult result, 
          Principal principal,
          Model model) throws Exception
  
  {   
      // ERROR - at this point, fieldA is null
  
  }
  }
  </code></pre>
  
  <p><strong>JSP</strong></p>
  
  <pre><code>    &lt;form:form  method="post" commandName="foo"&gt;
          &lt;fieldset&gt;
              &lt;legend&gt;Invest&lt;/legend&gt;
              &lt;div class="fm-req"&gt;
                  &lt;label for="fm-name"&gt;Field B&lt;/label&gt;    
                  &lt;spring:bind path="fieldB"&gt;
                      &lt;input id="fm-name" type="text" name="${status.expression}" value="${status.value}" &gt;&lt;/input&gt;
                  &lt;/spring:bind&gt;
              &lt;/div&gt;
              &lt;div id="fm-submit" class="fm-req"&gt;
                  &lt;INPUT type="submit" value="Submit" name="Submit" /&gt;        
              &lt;/div&gt;
          &lt;/fieldset&gt;
      &lt;/form:form&gt;                
  </code></pre>
  
  <p>I would have thought that the JSP gets the object created in newForm that has fieldA set ( and possibly fieldB ).  The user has the option to change fieldB and then hit submit.
  I've done lots of reading of the Spring docs and checked web sites, but cannot find out why the foo.fieldA is null on the update method in the controller.  </p>
  
  <p>From what I understand about Spring MVC this appears to be a standard pattern, but please feel free to correct me.</p>
  
  <p>Thanks in advance,</p>