Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js + Spring MVC. Save model

When I send a request from JSP FORM, the server side automatically parses data to my ModelObject. But when I send requests from Backbone save() my ModelObject is empty on Server Side. How can I do it like as JSP FORMs?

    @RequestMapping(value = "/member/ajax*", method = RequestMethod.POST)
    public void onSubmitAjax(Member member, HttpServletResponse response, HttpServletRequest request) throws Exception {
        //member is empty           
        memberManager.saveMember(member); 

    }

when I use GET it's working on Client Side:

@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;
    }
like image 387
SparX Avatar asked Dec 21 '11 16:12

SparX


1 Answers

I wrote a Spring MVC 3.1 backend for Backbone.JS Todo sample application. The code of the CRUD Controller may help you.

Based on your code sample, I think you should check that you have Jackson in your project dependencies, and use the following annotations for your onSubmitAjax method:

@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json") @ResponseStatus(HttpStatus.CREATED) @ResponseBody

You should also try RESThub, a nice Spring + Backbone.js stack provided with documentation, tutorial and code samples (Disclaimer: I am RESThub lead developer).

like image 192
Sébastien Deleuze Avatar answered Oct 11 '22 21:10

Sébastien Deleuze