当我从JSP FORM发送请求时,服务器端会自动将数据解析到我的ModelObject.但是当我从Backbone save()发送请求时,我的ModelObject在服务器端是空的.我如何像JSP FORM那样做?
@RequestMapping(value = "/member/ajax*",method = RequestMethod.POST) public void onSubmitAjax(Member member,HttpServletResponse response,HttpServletRequest request) throws Exception { //member is empty memberManager.saveMember(member); }
当我使用GET它在客户端工作:
@RequestMapping(value = "/member/ajax*",method = RequestMethod.GET) public @ResponseBody Member showForm(@RequestParam(required = false) Long id,HttpServletRequest request) throws Exception { Member member = memberManager.getMember(id); return member; }
解决方法
我为Backbone.JS Todo示例应用程序写了一个
Spring MVC 3.1 backend.
CRUD Controller的代码可以帮助您.
根据您的代码示例,我认为您应该检查您的项目依赖关系中的Jackson,并为您的onSubmitAjax方法使用以下注释:
@RequestMapping(method = RequestMethod.POST,consumption =“application / json”,produce =“application / json”)@ResponseStatus(HttpStatus.CREATED)@ResponseBody
您还应该尝试RESThub,一个很好的Spring Backbone.js堆栈,提供了文档,教程和代码示例(免责声明:我是RESThub主管开发人员).