Spring MVC中的Array @ModelAttribute扩展 [英] Array @ModelAttribute expansion in Spring MVC

查看:103
本文介绍了Spring MVC中的Array @ModelAttribute扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*我可能必须使用列表,但适用相同的原则.

* I may have to use a list but the same principle applies.

我正在尝试使用@ModelAttribute批注将数组绑定到表单.表中将填充数组的内容(数组中的每个元素对应于表中的一行).该数组可以填充数据,也可以在绑定时为空.用户可以向表中添加行(应该向表中添加元素).

I'm attempting to bind an array to a form using the @ModelAttribute annotation. A table is populated with the contents of the array (each element in the array corresponds to a row in the table). The array may be populated with data or may be empty when it's bound. The user can add rows to the table (which should add elements to the array).

我的问题是,如果在传递数组之前已经定义了元素,如何将其添加到数组中?提交表单后,这种情况会自动发生吗?

My question is how can elements be added to the array if it's already defined before I pass it in? Will this automagically happen when the form is submitted?

<body>
    <h1>Members</h1>
        <form:form action="configure" modelAttribute="members" method="post" id="member-form">
            <table id="member-table" class="table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody id="member-table-body">
                    <c:forEach items="${members}" var="member" varStatus="i" begin="0" >
                        <tr class="member">    
                            <td><form:input path="members[${i.index}].name" id="name${i.index}" /></td>
                            <td><a href="#" class="remove">Remove</a></td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
            <input type="submit" value="Save" id="submit" />
            <a href="#" id="add">Add Member</a>
            <a href="?f=">Reset List</a>
        </form:form>   
</body>

__

$(document).ready(function(){
$('#add').click(function(e) {
    e.preventDefault();
    //Add a member

    //Get the number of rows that are in the table
    var memberTable = $('#member-table-body');
    var numRows = memberTable.children('tr').length

    //Add row to the table.
    // memberTable.innerHTML(

    return false;
});
$('.remove').on('click', function(e) {
                e.preventDefault();
                $(this).parent().parent().remove();
                return false;
});

});

推荐答案

您需要创建一个包装列表的Model类,并在控制器中使用此Model.

You need to create a Model class wrapping the List and use this Model in your controller.

类似的东西:

package net.viralpatel.spring3.form;

import java.util.List;

public class ContactForm {

    private List<Contact> contacts;

    public List<Contact> getContacts() {
        return contacts;
    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;
    }
}

然后在JSP中将其用作:

And then use this in JSP as:

<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
    <tr>
        <td align="center">${status.count}</td>
        <td><input name="contacts[${status.index}].firstname" value="${contact.firstname}"/></td>
        <td><input name="contacts[${status.index}].lastname" value="${contact.lastname}"/></td>
        <td><input name="contacts[${status.index}].email" value="${contact.email}"/></td>
        <td><input name="contacts[${status.index}].phone" value="${contact.phone}"/></td>
    </tr>
</c:forEach>

请参阅本教程: Spring MVC:多行表单提交使用Bean列表

这篇关于Spring MVC中的Array @ModelAttribute扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆