Lolly
Lolly

Reputation: 36372

Spring MVC using AJAX

I am new to Spring MVC. I have an existing system which uses Spring MVC with SimpleController. On submit of a button there is a ajax call which in turn calls controller and returns modelview object.

At this scenario I have a question. This modelview object returns a view, a jsp page and also on success of ajax call there is a call to windows.href.location which redirects to another url. Can someone help me which of these two will get called?

Will the view's jsp will be rendered and then will get again redirected to url specified in success function?

Upvotes: 1

Views: 4308

Answers (2)

denov
denov

Reputation: 12688

Normally you don't want our ajax calls returning rendered pages (ie, JSPs) and rather they should follow RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1 Here's a controller from a project I'm working on.

@Controller
public class MilestoneController extends BaseRESTController {

    protected static final String ACCEPT_JSON = "Accept=application/json";

    @Resource private GuidelineService _guidelineService;


    @RequestMapping(value="/guideline/{id}/milestones", method= RequestMethod.GET, headers=ACCEPT_JSON)
    public @ResponseBody List<Milestone> getMilestones(@PathVariable("id") Long guidelineId) {
        return  _guidelineService.getGuideline(guidelineId).getMilestones();
    }


    @RequestMapping(value="/guideline/{id}/milestones/new", method= RequestMethod.GET, headers=ACCEPT_JSON)
    public @ResponseBody Milestone addMilestones(@PathVariable("id") Long guidelineId) {
        return _guidelineService.newMilestone(guidelineId);
    }


    @RequestMapping(value="/guideline/{id}/milestones", method={RequestMethod.POST, RequestMethod.PUT}, headers=ACCEPT_JSON)
    public @ResponseBody ResponseEntity<String> updateUpdateMilestone(@PathVariable("id") Long guidelineId, @RequestBody Milestone milestone) {
        _guidelineService.updateMilestone(guidelineId, milestone);
        return new ResponseEntity<String>(HttpStatus.ACCEPTED);
    }

}

If you have jackson in your classpath using @ResponseBody will render your return value to JSON. And @RequestBody will allow you to POST and PUT json from your client. In my updateUpdateMilestone() method you can see how I don't need to return a value so i'm just returning a 202 (HttpStatus.ACCEPTED).

To use what @Sotirios posted

$.ajax({
    type: "POST",  
    url: someUrl,
    data: someData, 
    success: function(data){  
        windows.href.location = someNewLocation;
    },
    error: function(X) { 
    }       
});

You controller method would need to be something like

    @RequestMapping(value="/somePage", method={RequestMethod.POST},
headers="Accept=application/json")
    public @ResponseBody String doStuff(@RequestBody SomeObject obj) {
        // do stuff here....
        return "viewName"
    }

without @ResponseBody the control will try to render the view by name.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

The @Controller returns a View or view name. The DispatcherServlet will use that View to render a jsp. It will write the rendered jsp to the HTTP response body. That's what your AJAX call will receive. So in a method like I assume you have

$.ajax({
    type: "POST",  
    url: someUrl,
    data: someData, 
    success: function(data){  
        windows.href.location = someNewLocation;
    },
    error: function(X) { 
    }       
});

the data variable in the success callback contains all the HTML from the rendered jsp that's contained in the HTTP response body. In the method like above, you don't do anything with data, although you have downloaded it all. You simply change the location of the page by reassigning windows.href.location. So what you will see is not the rendered jsp, but the webpage your someNewLocation points to.

Upvotes: 2

Related Questions