记录一下Springboot前后端传参方式
基础环境:
Idea2018.3.2
maven3.6.1
jsp
js,ajax
Springboot
代码:
js:
$(document).ready(function() {
//添加用户信息,点击事件
$('#submit_btn').click(function() {
var myDate = new Date();
var obj={};
obj.account=$("#account").val();
obj.password=$("#password").val();
obj.userName=$("#userName").val();
obj.age=$("#age").val();
obj.sex=$("#sex").val();
var description = "";
for(var i in obj){
var property=obj[i];
description+=i+" = "+property+"\n";
}
console.log(description);
$.ajax({
type: "POST",
url: "/UserController/insertUserInfo",
dataType: "json",
data:JSON.stringify(obj),
contentType : "application/json",
success: function (data) {
console.log(data)
},
error: function (e) {
console.log(e)
}
})
})
})
Controller:
@RequestMapping(value="/UserController/insertUserInfo",method = RequestMethod.POST)
@ResponseBody
public String insertUserInfo(@RequestBody(required=false) User user){
System.out.println(user);
user.setCreateTime(new Date());
user.setLastChangeTime(new Date());
userDao.saveUser(user);
System.out.println(user.toString());
return "chenggong";
}
页面截图: