controller中是幾個單獨的基本類型參數jquery
Spring MVC-controllerajax
1 @RequestMapping("update") 2 @ResponseBody//此註解不能省略 不然ajax沒法接受返回值 3 public Map<String,Object> update(Long num, Long id, BigDecimal amount){ 4 5 Map<String,Object> resultMap = new HashMap<String, Object>(); 6 if(num == null || id == null || amount == null){ 7 resultMap.put("result", "參數不合法!"); 8 return resultMap; 9 } 10 //xxx邏輯處理 11 resultMap.put("result", "操做成功"); 12 return resultMap; 13 }
jquery ajaxjson
1 var params = {}; 2 //params.XX必須與Spring Mvc controller中的參數名稱一致 3 //不然在controller中使用@RequestParam綁定 4 params.num = num; 5 params.id = id; 6 params.amount = amount; 7 $.ajax({ 8 async:false, 9 type: "POST", 10 url: "price/update",//注意路徑 11 data:params, 12 dataType:"json", 13 success:function(data){ 14 if(data.result=='SUCCESS'){ 15 alert("修改爲功"); 16 }else{ 17 alert("修改失敗,失敗緣由【" + data + "】"); 18 } 19 }, 20 error:function(data){ 21 alert(data.result); 22 } 23 });
controller中是參數是實體bean,bean中屬性都是基本數據類型數組
Spring MVC-controllerapp
1 @RequestMapping("add") 2 @ResponseBody//此處不能省略 不然ajax沒法解析返回值 3 public Map<String,Object> add(DataVo dataVo){ 4 Map<String, Object> result = null; 5 if(dataVo.getNum() == null || StringUtils.isBlank(dataVo.geId())){ 6 result = new HashMap<String, Object>(); 7 result.put("msg", "參數不合法!"); 8 return result; 9 } 10 //xxx業務邏輯處理 11 12 return result; 13 }
實體bean DataVoasync
1 public class DataVo { 2 /** 3 * 編號 4 */ 5 private Long num; 6 /** 7 * id 8 */ 9 private String id; 10 11 public Long getNum() { 12 return num; 13 } 14 public void setNum(Long num) { 15 this.num = num; 16 } 17 public String getId() { 18 return id; 19 } 20 public void setId(String id) { 21 this.id = id; 22 } 23 }
jquery ajaxthis
1 var params = {}; 2 params.num = $("#num").val(); 3 params.id = $("#id").val();//注意params.名稱 名稱與實體bean中名稱一致 4 $.ajax({ 5 type: "POST", 6 url: "price/add", 7 data:params, 8 dataType:"json", 9 // contentType: "application/json; charset=utf-8",//此處不能設置,不然後臺沒法接值 10 success:function(data){ 11 if(data.msg != ""){ 12 alert( data.msg ); 13 } 14 }, 15 error:function(data){ 16 alert("出現異常,異常緣由【" + data + "】!"); 17 } 18 });
controller中是參數是實體bean,bean中屬性有數組url
Spring MVC-controllerspa
1 @RequestMapping("add") 2 @ResponseBody//此處不能省略 不然ajax沒法解析返回值 3 public Map<String,Object> add(@RequestBody DataVo dataVo){//@RequestBody註解不能省略,不然沒法接值 4 Map<String,Object> resultMap = new HashMap<String, Object>(); 5 //業務邏輯處理 6 return resultMap; 7 }
實體 DataVo插件
1 public class DataVo { 2 3 4 private BigDecimal[] nums; 5 private String id; 6 7 public Long getId() { 8 return id; 9 } 10 11 public void setId(Long id) { 12 this.id = id; 13 } 14 15 public BigDecimal[] getNums() { 16 return nums; 17 } 18 19 public void setNums(BigDecimal[] nums) { 20 this.nums = nums; 21 } 22 23 }
jquery ajax 須要jquery json的插件 進行json序列化,我這裏使用了json.js
且配置
datatype:"json",
contentType: "application/json; charset=utf-8",
1 var params = {}; 2 params.nums = []; 3 params.id = $("#id").val();//parmas.參數名 注意與實體bean參數名稱相同 4 var prices = document.getElementsByName("prices");//prices 是name="prices"一組input標籤 5 for (var i = 0; i < prices.length; i++) { 6 params.nums[i] = prices[i].value; 7 } 8 $.ajax({ 9 type: "POST", 10 url: "price/add", 11 data:JSON.stringify(params),//json序列化 12 datatype:"json", //此處不能省略 13 contentType: "application/json; charset=utf-8",//此處不能省略 14 success:function(data){ 15 alert(data); 16 }, 17 error:function(data){ 18 alert(data) 19 } 20 });