1、以实体类方式接收
前端 ajax 提交数据:
function fAddObj() { var obj = {}; obj['objname'] = "obj"; obj['pid'] = 1 ; $.ajax({ url: 'admin/Obj/addObj.do', method: 'post', contentType: 'application/json', // 这句不加出现415错误:Unsupported Media Type data: JSON.stringify(obj), // 以json字符串方式传递 success: function(data) { console.log("success..."); }, error: function(data) { console.log("error..."); } }); }
springmvc 以model对象的形式接收:
@Controller @RequestMapping("/admin/Obj") public class ObjAction { // 注入操作类 @Autowired private ObjService objService ; @RequestMapping(value = "/addObj") @ResponseBody public String addObj(@RequestBody Obj obj) { this.objService.insertObj(obj); return "success"; } }
2、以Map接收
@Controller @RequestMapping("/admin/Obj") public class ObjAction { /** * 前端操作与上面相同 * @return */ @RequestMapping(value = "/updateAttr") @ResponseBody public String updateAttr(@RequestBody Map<String, String> map) { if(map.containsKey("id"){ Integer id = Integer.parseInt(map.get("id")); } if(map.containsKey("objname"){ String objname = map.get("objname").toString(); } if(map.containsKey("pid"){ Integer pid = Integer.parseInt(map.get("pid")); } // 操作 ... return "success"; } }