1中重定向后读取flash属性

1中重定向后读取flash属性

本文介绍了如何在Spring MVC 3.1中重定向后读取flash属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Spring MVC 3.1中重定向后读取flash属性。

I would like to know how to read a flash attributes after redirection in Spring MVC 3.1.

我有以下代码:

@Controller
@RequestMapping("/foo")
public class FooController {

  @RequestMapping(value = "/bar", method = RequestMethod.GET)
  public ModelAndView handleGet(...) {
    // I want to see my flash attributes here!
  }

  @RequestMapping(value = "/bar", method = RequestMethod.POST)
  public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
    redirectAttrs.addFlashAttributes("some", "thing");
    return new ModelAndView().setViewName("redirect:/foo/bar");
  }

}

我缺少什么?

推荐答案

使用模型,它应该预填充闪存属性:

Use Model, it should have flash attributes prepopulated:

@RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model model) {
  String some = (String) model.asMap().get("some");
  // do the job
}

或者你可以使用:

or, alternatively, you can use RequestContextUtils#getInputFlashMap:

@RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(HttpServletRequest request) {
  Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
  if (inputFlashMap != null) {
    String some = (String) inputFlashMap.get("some");
    // do the job
  }
}

P.S。您可以返回 handlePost 中返回新的ModelAndView(redirect:/ foo / bar);

P.S. You can do return return new ModelAndView("redirect:/foo/bar"); in handlePost.

编辑

JavaDoc说:

它没有提到 ModelAndView ,所以可能更改handlePost返回 redirect:/ foo / bar string或 RedirectView

It doesn't mention ModelAndView, so maybe change handlePost to return "redirect:/foo/bar" string or RedirectView:

@RequestMapping(value = "/bar", method = RequestMethod.POST)
public RedirectView handlePost(RedirectAttributes redirectAttrs) {
  redirectAttrs.addFlashAttributes("some", "thing");
  return new RedirectView("/foo/bar", true);
}

我使用 RedirectAttributes 在我的代码中使用 RedirectView model.asMap()方法,它运行正常。

I use RedirectAttributes in my code with RedirectView and model.asMap() method and it works OK.

这篇关于如何在Spring MVC 3.1中重定向后读取flash属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:44