springmvc后台接前端的参数,数组,集合,复杂对象等
参考地址:https://blog.csdn.net/feicongcong/article/details/54705933
常用的几种方式如下:
(1)数组:
后台
@ResponseBody @RequestMapping(value = "/ajaxsortPriority") public ResultDo ajaxsortPriority(@RequestParam("ids[]") Long[] ids) { ResultDo resultDo=new ResultDo(); int size=cmsBannerService.sortPriority(ids); if(size==ids.length){ resultDo.setSuccess(true); }else{ resultDo.setSuccess(false); } return resultDo; }
前端
var param=[]; $("#tb_order").find("td[name=‘id‘]").each(function(){ param.push($(this).text()); }) var ids={ids:param}; $.ajax({ cache: true, type: "GET", url: "/cmsBanner/ajaxsortPriority", dataType:"json", data:ids, async: false, success: function (data) {
(2)集合
后台
@RequestMapping(value = "/cfgRepayRemind", method = RequestMethod.POST) @ResponseBody public ResultDo<?> cfgRepayRemind( @RequestBody List<SysDictPojo> sysDictPojos //@RequestBody 前台请求的数据格式必须为json ) { ResultDo<?> resultDo = ResultDo.build(); try { icProjectRepayService.cfgRepayRemind(sysDictPojos); } catch (Exception e) { resultDo.setSuccess(false); } return resultDo; }
//pojo类public class SysDictPojo extends AbstractBasePojo { private Long id; private String key; private String value; private String description; }
前端
function cfgRepayRemind(ele) { var url = $(ele).attr("value"); var params = []; $("#repayRemindMobile").find("ul").each(function () { var id = $(this).find("input[name=‘id‘]").eq(0).val(); var value = $(this).find("input[name=‘value‘]").eq(0).val(); params.push({id: id, value: value});//id,value 为java bean里的属性,名字一致 }) $.ajax({ cache: true, type: "POST", url: url, data: JSON.stringify(params),// 指定请求的数据格式为json,实际上传的是json字符串 contentType: ‘application/json;charset=utf-8‘,//指定请求的数据格式为json,这样后台才能用@RequestBody 接受java bean dataType: "json", async: false, success: function (data) { if (data.success) { toastr.success("操作成功"); setTimeout(function () { location.reload(); }, 1000) } } }); }
(3) 单参数
前端
$.ajax({ type:"post", data:{total:‘100‘}, dataType:‘json‘, url:"http://127.0.0.1:8089/icProject/test", success:function () { } })
后台
@RequestMapping(value = "/test", method = RequestMethod.POST) @ResponseBody public String test(@RequestParam("total") String total ) { return null; }
(3)传多个参数
前端
var tagIds = [] $.ajax({ type: "POST", url: "/auth/childComment/createComment", data: { id: $("#sourceId").val(), courseId: $("#courseId").val(), classId: $("#commentClassId").val(), content: $("#updateContent").val(), childId: $("#childId").val(), classCatalogId: $("#classCatalogId").val(), tagId: tagIds },
后台
@PostMapping("createComment") @PreAuthorize("hasAnyAuthority(‘merchant:childComment:index‘)") @ResponseBody public ResultDo createComment( @RequestParam(value = "childId") Long childId, @RequestParam(value = "courseId") Long courseId, @RequestParam(value = "classId") Long classId, @RequestParam(value = "classScheduleId") Long classScheduleId, @RequestParam(value = "content") String content, @RequestParam(value = "tagId[]", required = false) List<Long> tagIds ) {
(4)表单数据序列化传参,ajax提交
前端
var params = $("#sysUserFrm").serialize(); var url = "/sysUser/settingSave" $.ajax({ cache: true, type: "POST", url: url, data: params, dataType: "json", async: false, success: function (data) {} })
后台
@RequestMapping(value = "/settingSave", method = RequestMethod.POST) @ResponseBody public ResultDo<?> settingSave(SysUserPojo sysUserPojo) {}
这样sysUserPojo也能接收到Bean,其实这里的$("#sysUserFrm").serialize() 就相当于组装的 json对象 { }
使用时必须先组装json对象{username:"carter" }
我的习惯是用 @RequestBody接受带List对象的对象或者List对象,因为js中有个push方法用来组合得到 List 比较简单
不使用contentType: “application/json”则data可以是对象,使用contentType: “application/json”则data只能是json字符串
使用@RequestBody(实际上接受的是json的字符串)
function logined() { $.ajax({ type: "POST", url: "/backend/logined", data:JSON.stringify({userName:"cater",password:"123456"}) ,//转成字符串 contentType: "application/json;charset=utf-8",//不使用contentType: “application/json”则data可以是对象,使用contentType: “application/json”则data只能是json字符串 success: function (data) {} }) }
原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/9357027.html
时间: 2024-10-29 00:52:20