前台传对象 到后台
var ajaxData = {};
for (var i = 0, len = saves.length; i < len; i++) {var save = saves[i];
var delta = this.table2form(save.data.toJson(true), save.data.defCols);
ajaxData = delta;
}
$.ajax({
"type" : "post",
"dataType" : "json",
"url" : this.BASE_URL + this.SAVE_URL,
"data" : ajaxData,
"success" : function(json) {
for (var i = 0, len = saves.length; i < len; i++) {
var save = saves[i];
save.data.applyUpdates();
}
if (success && $.isFunction(success)) {
success.call(this, json);
}
}
});
后台接收对象,使用@BeanParam
@POST
@Path("/saveUser")
@Produces(MediaType.APPLICATION_JSON)
public String save(@BeanParam User user){
System.out.println("-----------"+user.getUsername());
Session session = HibernateUtil.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.flush();
session.close();
return "success";
}
要对User做一些处理,@XmlRootElement(name = "user")这个一定不要忘了加
@XmlRootElement(name = "user")
public class User {
@FormParam(value="id")
private Integer id;
@FormParam(value="username")
private String username;
@FormParam(value="email")
private String email;
@FormParam(value="password")
private String password;
}
至此,对象就可以传过来了