后端获取前端数据三种方式
@RequestParam、
前端发出的请求: path?id=1
获取前端传来的非json格式的数据
// 单个数据,@RequestParam中的值一定要与前端属性名一致
public void test(@RequestParam("name)" String name){
System.out.println(params);
}
// 数组数据
@RequestMapper(value="path")
public void test(@RequestParam Map<String,Object> params){
System.out.println(params);
@PathVariable
前端发出的请求:path/{id};
@RequestMapper(value="path/{id}")
public void test(@PathVariable("id") int id){
System.out.println(id);
}
@RequestBody
获取前台出来的json格式的数据
$.ajax({
type: 'POST',//方法类型
url: url,
contentType: 'application/json',
data: {
"username":"小明",
"sex":"男",
"age":"18"
},
success: function (result) {
if (result == 200) {
$('#goodsModal').modal('hide');
swal({
title: swlMessage,
type: 'success',
showCancelButton: false,
confirmButtonColor: '#1baeae',
confirmButtonText: '返回商品列表',
confirmButtonClass: 'btn btn-success',
buttonsStyling: false
}).then(function () {
window.location.href = "/admin/goods";
})
} else {
$('#goodsModal').modal('hide');
swal(result.message, {
icon: "error",
});
}
;
}
});
// bean
public class User{
private String username;
private String sex;
..
}
// Controller中代码片段
@RequestMapper(value="path/{id}",method = RequestMethod.POST)
public void test(@RequestBody User user){
System.out.println(user);
}
标签:RequestParam,前端,public,获取,三种,void,path,id
From: https://www.cnblogs.com/b10100912/p/16727003.html