1. 程式人生 > >springMvc 前端用json的方式向後臺傳遞物件陣列

springMvc 前端用json的方式向後臺傳遞物件陣列

JSP
var vipFee= new Array;
//遍歷選中的物件

$("#feeList :checkbox:checked").each(function(i){
    vipFee.push({"enterpriseSeq":$(this).attr("enterpriseSeq"),"merchNo":$(this).val(),"serviceFee":$(this).attr("fqbFee")});
        });
//進行非同步
$.ajax({  
    type:"POST",  
    url: "addVipFeeList",  
    async:false
, dataType:"json", contentType:"application/json", // 指定這個協議很重要 data:JSON.stringify(vipFee), success:function(data){ //判斷是否是成功的返回的 if(data.success===true){ $("#feeList :checkbox:checked").each(function(i){ var FQBFee = parseFloat($(this).parent().siblings("td"
).find("input[name='fqbFee']").val()); $(this).parent().siblings("td").find("input").attr("readonly","readonly"); $(this).parent().siblings("td").find("input[name='fqbFee']").val(FQBFee.toFixed(3)); //將“取消定價”,進行顯示 $(this).parent().siblings("td").find("a[id='cancelA']"
).show(); //將“確定定價”進行隱藏 $(this).parent().siblings("td").find("a[id='relateA']").hide(); //取消掉選中的狀態 $(this).prop("checked", false); }); } } }); Controller @RequestMapping(value = Constants.ADMIN + "/addVipFeeList",method=RequestMethod.POST) @ResponseBody public Map<String,Object> addVipFeeList(@RequestBody List<VipFee> vipFee){ Map<String,Object> map=new HashMap<String,Object>(); try { //判斷物件是否為空 if (vipFee!=null&&vipFee.size()>0) { //進行遍歷並賦值 for (VipFee v:vipFee) { v.setReplacePrdId(Constants.PRODUCT_TYPE_FQB); } } //進行新增 vipFeeService.addVipFeeList(vipFee); map.put("success", true); } catch (CoreException e) { map.put("success", false); log.error(e.getCode(),e); map.put("errorMsg", e.getCode()); } return map; }