1、以实体类方式接收
前端 ajax 提交数据:
function fAddObj() {
var obj = {};
obj['objname'] = "obj";
obj['pid'] = 1 ;
$.ajax({
url: 'admin/Obj/addObj.do',
method: 'post',
contentType: 'application/json',
data: JSON.stringify(obj),
success: function(data) {
console.log("success...");
},
error: function(data) {
console.log("error...");
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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";
}
}