jsp页面添加一个集合数组到action(用序列化提交)

页面的js

//点击a标签增加删除
        var i=0;
        $("#a").on("click",function(){
            var $newtr = $("<tr  id='model'>"  
                        +"<td><input type='text' name='contactsList["+i+"].contactName' value=''></td>"  
                        +"<td><input type='text' name='contactsList["+i+"].contactTel' value=''></td>"  
                        +"<td><input type='text' name='contactsList["+i+"].contactFax' value=''></td>"  
                        +"<td><input type='text' name='contactsList["+i+"].contactEmail' value=''></td>" 
                        +"<td><input type='text' name='contactsList["+i+"].contactRole' value=''></td>"
                        +"<td ><a href='javascript:void(0);' class='del'>删除</a></td>"
                        +"</tr>");
              i++;
            $newtr.find(".del").click(function(){
                $(this).parents("tr").remove();
            });
            $("#fourdiv").append($newtr);            
        });

页面jsp

<div id="a" style="color:blue;cursor:pointer"><h4>添加一个联系人</h4></div>
            <div id="thirddiv">
                <table id="fourdiv">
                    <tr>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>传真</th>
                        <th>邮箱</th>
                        <th>职务</th>
                        <th>操作</th>
                    </tr>
                </table>
            </div>

action中接收直接用集合接收就好(遍历成对象来添加到数据库)

private List<Contacts> contactsList = new ArrayList<Contacts>();//页面接收联系人的数组
    public List<Contacts> getContactsList() {
        return contactsList;
    }

    public void setContactsList(List<Contacts> contactsList) {
        this.contactsList = contactsList;
    }
    //添加多条联系人
        for(Contacts contacts:contactsList){
            contacts.setCustomId(customs.getId());
            this.customsService.addContacts(contacts);
        }

 

posted @ 2015-12-21 11:56  夏末、初秋  阅读(565)  评论(0编辑  收藏  举报