简体   繁体   English

使用AJAX的Spring MVC

[英]Spring MVC using AJAX

I am new to Spring MVC. 我是Spring MVC的新手。 I have an existing system which uses Spring MVC with SimpleController. 我有一个使用Spring MVC和SimpleController的现有系统。 On submit of a button there is a ajax call which in turn calls controller and returns modelview object. 提交按钮后,会有一个ajax调用,该调用又调用控制器并返回modelview对象。

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. 这个modelview对象返回一个视图,一个jsp页面,并且在成功执行ajax调用后,还会调用Windows.href.location并将其重定向到另一个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? 是否将呈现视图的jsp,然后再次将其重定向到成功函数中指定的url?

The @Controller returns a View or view name. @Controller返回一个View或视图名称。 The DispatcherServlet will use that View to render a jsp . DispatcherServlet将使用该View呈现一个jsp It will write the rendered jsp to the HTTP response body. 它将呈现的jsp写入HTTP响应主体。 That's what your AJAX call will receive. 这就是您的AJAX呼叫将收到的。 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. 成功回调中的data变量包含HTTP响应正文中包含的所有呈现的jsp的HTML。 In the method like above, you don't do anything with data , although you have downloaded it all. 在上面的方法中,尽管已经下载了所有data ,但是您对data不做任何事情。 You simply change the location of the page by reassigning windows.href.location . 您只需重新分配windows.href.location即可更改页面的位置。 So what you will see is not the rendered jsp , but the webpage your someNewLocation points to. 因此,您看到的不是渲染的jsp ,而是someNewLocation指向的网页。

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. 通常,您不希望我们的Ajax调用返回呈现的页面(即JSP),而应该遵循RFC 2616-超文本传输​​协议-HTTP / 1.1。这是我正在研究的项目中的控制器。

@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. 如果使用@ResponseBody在类路径中有杰克逊,则会将您的返回值呈现为JSON。 And @RequestBody will allow you to POST and PUT json from your client. @RequestBody允许您从客户端进行POST和PUT JSON。 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). 在我的updateUpdateMilestone()方法中,您可以看到我不需要返回值,因此我只返回202(HttpStatus.ACCEPTED)。

To use what @Sotirios posted 使用@Sotirios发布的内容

$.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. 如果没有@ResponseBody,则控件将尝试按名称呈现视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM