Note that there are some explanatory texts on larger screens.

plurals
  1. POBind elements to a list with spring mvc ModelAttribute
    text
    copied!<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 wich 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 dinamically 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="user[0].name" type="text" /&gt; &lt;input name="user[0].surname" type="text" /&gt; &lt;input name="user[1].name" type="text" /&gt; &lt;input name="user[1].surname" type="text" /&gt; &lt;input name="user[2].name" type="text" /&gt; &lt;input name="user[2].surname" type="text" /&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 en up with this html:</p> <pre><code>&lt;input name="user[0].name" type="text" /&gt; &lt;input name="user[0].surname" type="text" /&gt; &lt;input name="user[2].name" type="text" /&gt; &lt;input name="user[2].surname" type="text" /&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 "correct" form. But is there another solution? I thought that a form like </p> <pre><code>&lt;input name="user[].name" type="text" /&gt; &lt;input name="user[].surname" type="text" /&gt; &lt;input name="user[].name" type="text" /&gt; &lt;input name="user[].surname" type="text" /&gt; </code></pre> <p>may solve the problem but then I realized: how can spring know wich name comes with wich surname.</p> <p>Any idea on a better solution?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload