简体   繁体   English

使用 spring mvc ModelAttribute 将元素绑定到列表

[英]Bind elements to a list with spring mvc ModelAttribute

The question is based on getting a spring model attribute list element using an index from javascript on which explains that each name of the html element must contain an index.该问题基于使用来自 javascript 的索引获取 spring 模型属性列表元素,该索引解释了 html 元素的每个名称必须包含一个索引。 Example user[0]示例用户[0]

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:让我举个例子:我们添加了 3 个用户,所以表单现在将包含 3 个输入:

<input name="user[0].name"    type="text" />
<input name="user[0].surname" type="text" />
<input name="user[1].name"    type="text" />
<input name="user[1].surname" type="text" />
<input name="user[2].name"    type="text" />
<input name="user[2].surname" type="text" />

If we submit this, we'll have 3 users each having a name and a surname.如果我们提交这个,我们将有 3 个用户,每个用户都有一个名字和一个姓氏。 But, the problem comes when we try to remove the user[1] because I end up with this html:但是,当我们尝试删除 user[1] 时问题就出现了,因为我最终得到了这个 html:

<input name="user[0].name"    type="text" />
<input name="user[0].surname" type="text" />
<input name="user[2].name"    type="text" />
<input name="user[2].surname" type="text" />

If we submit this form, springs still creates 3 users but the user[1] will have null values for the name and surname.如果我们提交此表单,springs 仍会创建 3 个用户,但 user[1] 的 name 和 surname 将为空值。

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我认为像这样的形式

<input name="user[].name"    type="text" />
<input name="user[].surname" type="text" />
<input name="user[].name"    type="text" />
<input name="user[].surname" type="text" />

may solve the problem but then I realized: how can spring know which name comes with which surname.可能会解决问题,但后来我意识到:spring 怎么知道哪个名字来自哪个姓氏。

Any idea on a better solution?关于更好的解决方案的任何想法?

Finally I added all the inputs without the indexed variable (in following the code now i'll add the containers that were missing before):最后,我添加了所有没有索引变量的输入(在下面的代码中,我将添加之前丢失的容器):

<div class="container">
    <input name="name"    type="text" />
    <input name="surname" type="text" />
</div>
<div class="container">
    <input name="name"    type="text" />
    <input name="surname" type="text" />
</div>
<div class="container">
    <input name="name"    type="text" />
    <input name="surname" type="text" />
</div>

So before submitting the form I use updateIndexedInputNames($(".container"), "user");所以在提交表单之前我使用updateIndexedInputNames($(".container"), "user");

And let me share the code of updateIndexedInputNames:让我分享 updateIndexedInputNames 的代码:

function updateIndexedInputNames($container, name){
    $container.each(function(containerIndex, container){
        $(this).find("input,select").each(function(index, element){
            $(element).attr("name", name+"["+containerIndex+"]."+element.name);
        });
    });
}

I don't think that this is a better solution than the one suggested by @sp00m but it works too.我不认为这是比@sp00m 建议的更好的解决方案,但它也有效。

Hope this helps somebody!希望这对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM