表单提交
此处的表单时 -使用JSON.stringify()函数将数组转换成json类型提交后台,后台使用@RequestBody User user接受处理
页面js
//新增提交按钮 $("#buildsubmit").click(function() { var param = $(".form").serializeJson(); $.ajax({ type: 'post', url: path + "/web/member/save.do", contentType: "application/json", dataType: 'json', data: JSON.stringify(param), success: function(data) { }, }); } });
后端代码
@RequestMapping(value = "/save", method = RequestMethod.POST) public GeneralResponse save(@RequestBody @Valid MemberInsertDetail member, BindingResult bindingResult) throws JsonProcessingException { if (bindingResult.hasErrors()) { throw new ErrParamException(); } boolean flag = false; flag = memberService.save(member); }
表单提交二
使用.serialize()方法 提交表单内容;
1、可以后台使用 request.getParamter("对应字段的name")获取参数;
2、也可以使用 Model mdel 的POJO接受。(name要一一对应起来)
- 格式:var data = $("#formID").serialize();
- 功能:将表单内容序列化成一个以&拼接的字符串,键值对的形式,name1=val1&name2=val2&,空格以%20替换。
页面JS
function sub(){ $.ajax({ type:"post", url:"/restaurant/addEmployees.do", data:$("#form").serialize(), dataType :"json", success:function(data){ if(!data.success){ } }); }
页面html代码:
后台代码接收方式一
含有单个的checkbox参数接收
@RequestMapping("/addEmployees") @ResponseBody public ResultaddEmployees(HttpServletRequest request) { String phone = request.getParameter("phone"); String password = request.getParameter("password"); String username = request.getParameter("username"); 身份单checkbox接收。如果是复选框多个checkbox,则用数组String[] 接收。 String checkbox = request.getParameter("checkbox"); }
后台代码接收方式二
@RequestMapping(value="/addCustomer",method=RequestMethod.POST) @ResponseBody public LogisticsResult addCustomer(@Valid CustomerInfo customer,BindingResult result ){ 如果是复选框多个checkbox,则在pojo中 用与checkbox的name一样的 数组接收。 如: String[] checkbox; }
接收List
1、页面js代码:
var idList = new Array(); idList.push(“1”); idList.push(“2”); idList.push(“3”); var isBatch = false; $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes", dataType: 'json', data: {"idList":idList,"isBatch":isBatch}, success: function(data){ … }, error: function(res){ … } });
2、Controller方法:
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=deleteCatalogSchemes") @ResponseBody public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") ListidList,Boolean isBatch) { … } }
接收List
1、User实体类:
public class User { private String name; private String pwd; //省略getter/setter }
2、页面js代码:
var userList = new Array(); userList.push({name: "李四",pwd: "123"}); userList.push({name: "张三",pwd: "332"}); $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(userList),//将对象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
3、Controller方法:
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody ListuserList) { … } }
如果想要接收User[]数组,只需要把saveUsers的参数类型改为@RequestBody User[] userArray就行了。
接收List
1、页面js代码(不需要User对象了):
var userList = new Array(); userList.push({name: "李四",pwd: "123"}); userList.push({name: "张三",pwd: "332"}); $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(userList),//将对象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
2、Controller方法:
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody List
接收User(bean里面包含List)集合参数:
1、User实体类:
public class User { private String name; private String pwd; private Listcustomers;//属于用户的客户群 //省略getter/setter }
2、页面js代码:
var customerArray = new Array(); customerArray.push({name: "李四",pwd: "123"}); customerArray.push({name: "张三",pwd: "332"}); var user = {}; user.name = "李刚"; user.pwd = "888"; user. customers = customerArray; $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(user),//将对象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
3、Controller方法:
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody User user) { Listcustomers = user.getCustomers(); … } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。