title: How to pass Spring MVC model to controller from a loop in JSP
body: <p>Regarding Spring MVC and JSP.</p>
  
  <p>My JSP iterates over a list of users.  Each user is displayed in a row of a table.</p>
  
  <p>On each row is an Edit and a Delete button.</p>
  
  <p>When either button is clicked I want to call the associated controller method passing the <strong>user model</strong> for the associated row.</p>
  
  <p>To do this, I've added a form within the loop.  However, when I click the button, the user model values are always null in the controller.</p>
  
  <ul>
  <li>Note: I know I can do this by using the ID of the model but I am
  trying to pass the model itself.  If it isn't possible I'd like to understand why not.</li>
  </ul>
  
  <p>Here is my controller method:</p>
  
  <pre><code>@RequestMapping(value = "/user/delete", method = RequestMethod.POST)
  public ModelAndView deleteUser(@ModelAttribute("user") User user) {
      ....
  }
  </code></pre>
  
  <p>Here is my JSP    </p>
  
  <pre><code>&lt;c:forEach var="user" items="${users}"&gt;
      &lt;form:form action="/" id="user${user.id}Form" method="post" commandName="user"&gt;
          &lt;tr&gt;
              &lt;td class="col-xs-8 col-sm-8 col-md-8 col-lg-8"&gt;${user.id}: ${user.firstName} ${user.lastName}&lt;/td&gt;
              &lt;td class="col-xs-4 col-sm-4 col-md-4 col-lg-4"&gt;
                  &lt;div class="btn-toolbar pull-right" role="btn-toolbar"&gt;
                      &lt;div class="btn-group" role="group"&gt;
                          &lt;input class="btn btn-default btn-default"
                                 type="Submit"
                                 value="Edit"
                                 id="${user.id}"
                                 onclick="document.getElementById('user${user.id}Form').action='${pageContext.request.contextPath}/user/updateLoad'"&gt;
                      &lt;/div&gt;
                      &lt;div class="btn-group" role="group"&gt;
                          &lt;input class="btn btn-default btn-default"
                                 type="Submit"
                                 value="Delete"
                                 id="${user.id}"
                                 onclick="document.getElementById('user${user.id}Form').action='${pageContext.request.contextPath}/user/delete'"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/form:form&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>What would I need to do to pass the user model to the controller please?</p>
  
  <p>I can get the values in the controller if I do the following, however, I don't consider this to be a practical solution because of the overhead.</p>
  
  <pre><code>    &lt;c:forEach var="user" items="${users}"&gt;
          &lt;form:form action="/" id="user${user.id}Form" method="post" commandName="user"&gt;
              &lt;input type="hidden" name="id" value="${user.id}"/&gt;
              &lt;input type="hidden" name="firstName" value="${user.firstName}"/&gt;
              &lt;input type="hidden" name="lastName" value="${user.lastName}"/&gt;
              &lt;input type="hidden" name="phone" value="${user.phone}"/&gt;
              &lt;input type="hidden" name="address" value="${user.address}"/&gt;
  </code></pre>