Springmvc后台接前台数组,集合,复杂对象本⼈转载⾃:
return "redirect:/icProject/index"; 重定向到请求
model.addAttribute("page", page);
return "icProject/index";跳转到页⾯
1.ajax提交,将数组封装成json,后台⽤List<Integer>接
2.form提交
⽅法⼀:
前台form
<form action="/sysUser/create" method="post">
<input name="roleId" >
<input name="roleId" >
<input name="roleId" >
</form>
后台
public String create(Model model,@Valid SysUserPojo sysUserPojo, BindingResult bindingResult, final RedirectAttributes
redirectAttributes,@RequestParam(value="roleId", required = false) List<Long> roleIds) {
}
⽅法⼆:
前台form
<form action="/sysUser/create" method="post">
<input name="menuIds[0]" >
<input name="menuIds[1]" >
<input name="menuIds[2]" >
</form>
后台
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(
@Valid SysRoleDto sysRoleDto, BindingResult bindingResult, final RedirectAttributes redirectAttributes) {
}
public class SysRoleDto extends SysRolePojo {
@NotNull
private List<Long> menuIds;
public SysRoleDto() {
}
public List<Long> getMenuIds() {
return menuIds;
}
public void setMenuIds(List<Long> menuIds) {
}
}
⽅法三
@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) {
⽅法4
@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;
}
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 () {
}, 1000)
}
}
});
}
public class SysDictPojo extends AbstractBasePojo {
private Long          id;
private String        key;
private String        value;
private String        description;
}
PS
场景⼀
$.ajax({
type:"post",
<span >  </span>    data:{total:'100'},
dataType:'json',
url:"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;
}
后台可以取到total=100,发现默认是以application/x-www-form-urlencoded 格式提交请求数据的场景⼆
function testpost1() {
$.ajax({
type:"post",
data:{total:'100',icProjectPojo:{totalAmount:10000}},
dataType:'json',
url:"127.0.0.1:8089/icProject/test",
success:function () {
springmvc考试选择题}
})
}
icProjectPojo[totalAmount]:
10000 是以这样的key-value 形式提交数据的,⽽不是json对象的⽅式
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request, @RequestParam("total") String total, IcProjectPojo icProjectPojo      ) {
return null;
}
后台接收时,IcProjectPojo这个bean⾥的totalAmount明显是就收不到数据的,如果有⼀个属性叫 icProjectPojo[totalAmount] ,那他会接受到数据所以传对象要⽤@RequestBody (⽅法⾥只有⼀个,即必须是⼀整个对象)和
data:JSON.stringify(params),//此处也不⽤给对象取key,因为后台的⽅法⾥@RequestBody 只允许注解⼀个对象
contentType: "application/json",
不使⽤contentType: “application/json”则data可以是对象
使⽤contentType: “application/json”则data只能是json字符串
⽅法五:表单数据序列化,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) {}
})

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。