(Ajax传json格式数据到controller中,自动转为java对象)
如果controller方法中的接收参数为java对象的话,则自动会将传过来的json格式按照属性名一 一对应赋值给新创建的java对象。
例子:
ajax发送的json数据:
$.ajax({
url: "user_login",
data: {
"uno": "9527",
"upass": "12345",
"udelete":"0",
"utest":"test data"
},
type: "post",
dataType: "json",
success: function (result) {
alert(JSON.stringify(result))
}
})
controller方法请求参数:
java对象的属性
public class User {
private Integer id;
private String uno;
private String uname;
private String upass;
private Integer udelete;
}
只有属性名和json的key值对应,则会自动赋值给对象,其余为null。
如上述例子,如果想要接受非对象属性值传过来的json数据,则直接在controller方法的接收参数中添加新的参数。即:
public MessageResult login(User user,String utest)