在JSP页面开发中,我们常常会用到form表单做数据提交,由于以前一直只是使用 form表单提交单个对象,只要表单文本域的name值和接收的对象的属性名一致,那么传值就没有什么问题。不过,在前几天的开发任务中,遇到了需要批量 传递对象,也就是需要传递对象数组,在此做个总结。今天又遇到需要向后台传递数组,便一并写下来吧。

1、ajax传递普通数组
前台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var deleteNum= [];//定义要传递的数组
deleteNum.push("1");
deleteNum.push("2");
deleteNum.push("3");//向数组中添加元素
 
$.ajax({
    type:"post",
    url:"deleteNum.do",
    data:{deleteNum:deleteNum},
    traditional: true,//必须指定为true
    success:function(data){
        if(data.success){
            deleteNum = [];
        }
    }
});

后台代码

1
2
3
4
5
public ActionResult deleteNum(String[] deleteNum){
     
    //这个时候已经得到了deleteNum数组值
    return Json(new {status=1});
}

form表单提交自定义对象数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<form id="form" name="form"  method="post">
  <table>
    <tr>
      <td><input type="text" name="user[0].name"/></td>
      <td><input type="text" name="user[0].password"/></td>
    </tr>
    <tr>
      <td><input type="text" name="user[1].name"/></td>
      <td><input type="text" name="user[1].password"/></td>
    </tr>
    <tr>
      <td><input type="text" name="user[2].name"/></td>
      <td><input type="text" name="user[2].password"/></td>
    </tr>
  </table>
</form>

之后在form表单提交给后台时,需要先定义一个对象,当然,别忘了定义User对象,属性为name,password,重点是下面这个用于接收form表单提交的对象数组,如果还有其他对象数组需要接收,那在这个类里面再定义相应的ArrayList属性即可。

/**
* 类型描述
* 表单列表对象 用于接收form表单提交的对象数组
* @since 2016-2-25
* @author 古时一轮月
*
*/
public class FormListObject {
private ArrayList<User> userlist;
public ArrayList<User> getUserlist() {
return userlist;
}
public void setUserlist(ArrayList<User> userlist) {
this.userlist= userlist;
}
}

定义好该对象之后,在controller层接收就可以了

1
2
3
public AjaxResult saveOrUpdateUser(FormListObject list){
    List<User> userlist = list.getUserlist(); //你会看到你想要的
}

转自:http://blog.csdn.net/u014252157/article/details/50751302

form表单传递对象数组的更多相关文章

  1. ajax传递数组、form表单提交对象数组

    在JSP页面开发中,我们常常会用到form表单做数据提交,由于以前一直只是使用form表单提交单个对象,只要表单文本域的name值和接收的对象的属性名一致,那么传值就没有什么问题.不过,在前几天的开发 ...

  2. form表单传递下拉框的Value和Text值,不适用Jquery传递

    同时获取下拉框的Value和Text值的解决办法:添加一个<input type="text" >文本框,用户选中一项后就将该项的value值给他 然后接受页面获取该文 ...

  3. select空间提交form表单传递参数

    如下, 到了 <form name="modelForm" action="/portal/defectinfo/toDefectPage?projectname= ...

  4. 参数传递的四种形式----- URL,超链接,js,form表单

    什么时候用GET,  查,删, 什么时候用POST,增,改  (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:/ ...

  5. ***四种参数传递的形式——URL,超链接,js,form表单

    什么时候用GET,  查,删 什么时候用POST,增,改  (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:// ...

  6. 第十七篇 JS验证form表单

    JS验证form表单   这节课做一个实际的,项目里会遇到的东西,例如登录页面,我们输入‘用户名’和‘密码’或者‘手机号’还有‘验证码’等等,它都会做一个前端验证,比如验证码,是6位有效数字组成,那么 ...

  7. js将form表单序列化[json字符串、数组、对象]

    1.序列化为字符串 $("#Form").serialize();//name=zhangsan&sex=1&age=20   2.序列化为数组 var formD ...

  8. Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据、Ajax文件上传)

    form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multi ...

  9. form表单序列化为Jquery对象

    <form id="DailyFinancial" > @*class="form-inline"*@ <div class="fo ...

随机推荐

  1. Android 自定义View - 饼图

    最近有看到一个自定义等分圆的View,自己尝试做了一个类似的,效果图如下图(1)所示: 图(1) 实现方法:自定义View-ColorCircle,需要的知道的值有圆的半径,等分个数以及扇形颜色. / ...

  2. foo、bar美国版的张三李四

    不管看javascript还是其他语言举例,经常看到使用foo和bar来充当变量.那么究竟foo.bar是什么鬼? 一说:foo 和 bar 组合在一起所构成的 foobar 应该最能反映其原始的意思 ...

  3. table中checkbox选择多行

    页面代码 <table id="addressTable" class="ui-jqgrid-htable ui-common-table table table- ...

  4. 使用NodeJS将文件或图像上传到服务器

    原文链接:http://www.codeceo.com/article/nodejs-upload-file-to-server.html

  5. nvm命令行操作命令

    1,nvm nvm list 是查找本电脑上所有的node版本 - nvm list 查看已经安装的版本 - nvm list installed 查看已经安装的版本 - nvm list avail ...

  6. Git简介【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 Git简介 Git是什 ...

  7. Matplotlib 练习题

    1. 绘制一个二维随机漫步的图形 直接上代码: %pylab inline nsteps = 1000 draws = np.random.randint(-1,2,size=(2,nsteps)) ...

  8. 遗传算法 Genetic Algorithm

    2017-12-17 19:12:10 一.Evolutionary Algorithm 进化算法,也被成为是演化算法(evolutionary algorithms,简称EAs),它不是一个具体的算 ...

  9. 数据模板--DataTemplate

    DataTemplate 的 "DataType" 通常就是 “ViewModel” ——视图模型(亦可不恰当称之为:数据模型) <DataTemplate DataType ...

  10. JS级联下拉框

    //Ajax级联获取SDKfunction GetDropDownList(parent_ddlID, fill_dllID, url, param) {    this.pId = parent_d ...