springMVC怎么获取前台post过来的对象

2024-11-29 01:09:40
推荐回答(2个)
回答1:

前台:functionpost4(){varpara={'user.username':"name",'user.password':"password"};$.ajax({url:'vote/post4.html',data:para,success:function(data){alert(data);}});}后台:@RequestMapping(value="/post4")publicvoidpost4(@ModelAttribute("user")Useruser,PrintWriterout){System.out.println(user.getPassword());out.write(user.getUsername());out.flush();out.close();}可是我这么写报空啊,要加什么别的,或者架包么更精细的匹配你可以实现一个springmvc的WebArgumentResolver接口,然后配在的子标签里:)

回答2:

@RequestMapping(value="/test",method=RequestMethod.POST)
public @ResponseBody Map testPostJson(
@RequestBody UserForm userForm,
BindingResult bindingResult) {

Map map = new HashMap();
if (bindingResult.hasErrors()) {
map.put("errorCode", "40001");
map.put("errorMsg", bindingResult.getFieldError().getDefaultMessage());
}

map.put("user", userForm);
return map;
}