有没有办法在Spring MVC Controller中使用响应重定向?将ModelAndView与ResponseBody一起使用时,只能使用空体重定向。
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ModelAndView redirectWithResponse(@RequestParam("redirect_url") String redirectUrl,
@RequestParam("data") String data) {
MyJSONResponse response = new MyJSONResponse(data);
ModelAndView modelAndView = new ModelAndView("redirect:"+redirectUrl);
modelAndView.addObject(response);
return modelAndView;
}
答案 0 :(得分:1)
您必须将它们复制到RedirectAttributes
:
public ModelAndView redirectWithResponse(@RequestParam("redirect_url")
String redirectUrl, @RequestParam("data") String data,
RedirectAttributes redir) {
...
MyJSONResponse response = new MyJSONResponse(data);
ModelAndView modelAndView =
new ModelAndView("redirect:" + redirectUrl);
redir.addFlashAttribute("object", response);
return modelAndView;
...
}
flash属性的范围是请求,它们将在重定向后继续存在,但在使用后将被删除