Spring MVC AngularJS - Post Redirect



我相信我只是错过了一些明显的东西。

有一个有效的帖子,但它给了我 HTML 作为响应。通常,如果我实际上尝试在当前页面上加载更多信息,那就太好了。但我真的希望它重定向。

主服务中的帖子.js

searchOpportunities : function(title, location) {
                                return $http
                                        .post(
                                                '/',
                                                $.param({
                                                    title : title,
                                                    location : location
                                                }),
                                                {
                                                    headers : {
                                                        'Content-Type' : 'application/x-www-form-urlencoded'
                                                    }
                                                })
                            }

回应

@RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView search_Post(@RequestParam(value = "title", required = true) String title, @RequestParam(value = "location", required = true) String location) {
        ModelAndView searchView = new ModelAndView("search");
        searchView.addObject("searchTitle", title);
        searchView.addObject("searchLocation", location);
        return searchView;
    }

澄清一下:

我希望页面在发送帖子后更改为搜索视图。现在它只是发送 HTML 作为响应...但我想使用正确的对象"搜索标题"和"搜索位置"重定向

因为您只是在POST之后渲染searchView视图,而不是重定向到某个地方。如果您确实要Redirect,请使用redirect:前缀。假设您有一个/search端点,并在成功POST / 后重定向到该端点,然后:

@RequestMapping(value = "/", method = RequestMethod.POST)
public String search_Post(@RequestParam(value = "title", required = true) String title, @RequestParam(value = "location", required = true) String location) {
    ...
    return "redirect:/search";
}

如果需要,可以在/search终结点中呈现searchView。在此处阅读有关重定向视图的更多信息。

最新更新