title: Spring form ID field not populated on POST
body: <p>In the below Spring form, my object's ID field is populated, but when I receive the submission in the controller method, all of the form's fields are populated except for the ID field. I've quintuple-checked that the field type and getter/setter types are all the same non-primitive, as I've seen many of the other questions on SO similar to this and that seems to be the common issue. The controller doesn't have any method-level @ModelAttributes, so it isn't being populated otherwise.</p>
  
  <p>Here's the declaration of the POST method, as I debugged it on the first containing line and found that the form's id field is empty:</p>
  
  <pre><code>    @RequestMapping(value="/{orgId}", method=RequestMethod.POST)
  public String editOrganizationPost(@PathVariable int orgId,
          @Valid @ModelAttribute(ORG_FORM) OrganizationForm orgForm,
          BindingResult result, RedirectAttributes att,
          HttpServletRequest request) {
  </code></pre>
  
  <p>Here's the form object:</p>
  
  <pre><code>public class OrganizationForm {
  
  private Integer id;
  @NotBlank
  private String name;
  @NotBlank
  private String description;
  private Set&lt;User&gt; users;
  
  int moveToOrganizationId = 0;
  String moveToOrganizationName;
  int[] moveFromOrganizationUserSelect = null; // List of selected users
                                                  // to be moved to a new
                                                  // organization
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public Set&lt;User&gt; getUsers() {
      return users;
  }
  
  public void setUsers(Set&lt;User&gt; users) {
      this.users = users;
  }
  
  public int getMoveToOrganizationId() {
      return this.moveToOrganizationId;
  }
  
  public void setMoveToOrganizationId(int moveToOrganizationId) {
      this.moveToOrganizationId = moveToOrganizationId;
  }
  
  public String getMoveToOrganizationName() {
      return this.moveToOrganizationName;
  }
  
  public void setMoveToOrganizationName(String moveToOrganizationName) {
      this.moveToOrganizationName = moveToOrganizationName;
  }
  
  public int[] getMoveFromOrganizationUserSelect() {
      return this.moveFromOrganizationUserSelect;
  }
  
  public void setMoveFromOrganizationUserSelect(
          int[] moveFromOrganizationUserSelect) {
      this.moveFromOrganizationUserSelect = moveFromOrganizationUserSelect;
  }
  
  public boolean isNew() {
      return this.id == null || this.id == 0;
  }
  
  }
  </code></pre>
  
  <p>Here is the markup from the JSP file:</p>
  
  <pre><code>&lt;form:form method="post" action="${submitUrl}" commandName="organizationForm"&gt;
                  &lt;form:errors path="*" /&gt;
                  &lt;form:hidden path="id" /&gt;
                  &lt;table class="adminTable editContent"&gt;
                      &lt;tr class="bg_lgtGrey"&gt;
                          &lt;td&gt;&lt;fmt:message
                                  key="manageOrganizations.organizationForm.name" /&gt;:&lt;/td&gt;
                          &lt;td&gt;&lt;form:input path="name" cssClass="inputbox"
                                  tabindex="4" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr class="bg_lgtGrey"&gt;
                          &lt;td&gt;&lt;fmt:message
                                  key="manageOrganizations.organizationForm.description" /&gt;:&lt;/td&gt;
                          &lt;td&gt;&lt;form:textarea path="description"
                                  cssClass="inputbox" tabindex="4" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td colspan="2" align="right"&gt;&lt;c:choose&gt;
                                  &lt;c:when test="${!organizationForm.new}"&gt;
                                      &lt;input type="submit" class="btn btn-primary"
                                          id="submit_button" value="Update" /&gt;
                                  &lt;/c:when&gt;
                                  &lt;c:otherwise&gt;
                                      &lt;input type="submit" class="btn btn-primary"
                                          id="submit_button" value="Create" /&gt;
                                  &lt;/c:otherwise&gt;
                              &lt;/c:choose&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/form:form&gt;
  </code></pre>
  
  <p>And here is the generated HTML:</p>
  
  <pre><code>&lt;form id="organizationForm" action="/admin/organizations/1" method="post"&gt;
  
                  &lt;input id="id" name="id" type="hidden" value="1"&gt;
                  &lt;table class="adminTable editContent"&gt;
                      &lt;tbody&gt;&lt;tr class="bg_lgtGrey"&gt;
                          &lt;td&gt;Organization Name:&lt;/td&gt;
                          &lt;td&gt;&lt;input id="name" name="name" class="inputbox" tabindex="4" type="text" value="Organization1"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr class="bg_lgtGrey"&gt;
                          &lt;td&gt;Organization Description:&lt;/td&gt;
                          &lt;td&gt;&lt;textarea id="description" name="description" class="inputbox" tabindex="4"&gt;&lt;/textarea&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td colspan="2" align="right"&gt;
  
                                      &lt;input type="submit" class="btn btn-primary" id="submit_button" value="Update"&gt;
  
  
                              &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/tbody&gt;&lt;/table&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>I used Chrome devtools to catch the POST data and here it is:</p>
  
  <pre><code>id=1&amp;name=Organization1&amp;description=
  </code></pre>
  
  <p>...yet at the breakpoint at the first line of the editOrganizationPost method, the form.id field is set to 0.</p>
  
  <p>I have spent forever trying to figure out why it will bind the name and description but not the ID when sent. I could obviously just inject the ID from the path variable but I am dumbfounded as to why it wouldn't just populate the field naturally.</p>