请我通过 ajax 将数据插入到我的数据库中,在该页面中我还插入了一个记录表,并且每次添加数据时我都想更新表的内容。这就是我所做的:
@RequestMapping(value="/add_record",method=RequestMethod.POST)
public @ResponseBody String addUser(@ModelAttribute User us,BindingResult result,ModelMap model){
userservice.AddUser(us);
model.addAttribute("usersystem", userservice.getAllUsers()); // <== Here the problem
return returnText = "you succefully add a record !" ;
}
jQuery函数;
function doAjaxPost() {
var frm = $('#ajf');
frm.submit(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/ajouter_user",
data: frm.serialize(),
success: function(response){
// we have the response
$('#info').html(response);
},
error: function(e){
alert('Error: ' + e);
}
});
});
}
我不知道如何将控制器添加的属性返回到我的jsp页面,所以我总是刷新页面才能看到新记录。谢谢
如果我用这种方式怎么办:
@RequestMapping(value="/add_record",method=RequestMethod.POST)
public @ResponseBody ModelAndView addUser(@ModelAttribute User us,BindingResult result,ModelMap model){
ModelAndView model = new ModelAndView("jspName");
userservice.AddUser(us);
model.addObject("usersystem", userservice.getAllUsers()); // <== Here the problem
return model;
}
我可以在 ajax 调用中接收这个模型吗?