title: Bind elements to a list with spring mvc ModelAttribute
body: <p>The question is based on <a href="https://stackoverflow.com/questions/13358950/getting-a-spring-model-attribute-list-element-using-an-index-from-javascript">getting a spring model attribute list element using an index from javascript</a> on which explains that each name of the html element must contain an index. Example user[0]</p>
  <p>I have a form that allows the user to dynamically add or remove elements from the list before submitting it. Let me put an example: we add 3 users, so the form will now contain 3 inputs:</p>
  <pre><code>&lt;input name=&quot;user[0].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[0].surname&quot; type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[1].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[1].surname&quot; type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[2].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[2].surname&quot; type=&quot;text&quot; /&gt;
  </code></pre>
  <p>If we submit this, we'll have 3 users each having a name and a surname. But, the problem comes when we try to remove the user[1] because I end up with this html:</p>
  <pre><code>&lt;input name=&quot;user[0].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[0].surname&quot; type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[2].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[2].surname&quot; type=&quot;text&quot; /&gt;
  </code></pre>
  <p>If we submit this form, springs still creates 3 users but the user[1] will have null values for the name and surname.</p>
  <p>I know that I can handle input names to always have a &quot;correct&quot; form. But is there another solution? I thought that a form like</p>
  <pre><code>&lt;input name=&quot;user[].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[].surname&quot; type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[].name&quot;    type=&quot;text&quot; /&gt;
  &lt;input name=&quot;user[].surname&quot; type=&quot;text&quot; /&gt;
  </code></pre>
  <p>may solve the problem but then I realized: how can spring know which name comes with which surname.</p>
  <p>Any idea on a better solution?</p>