How to read flash attributes after redirection in

2020-01-26 03:46发布

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

I have the following code:

@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");
  }

}

What I am missing?

3条回答
不美不萌又怎样
2楼-- · 2020-01-26 04:12

Try this:

@Controller
public class FooController
{
    @RequestMapping(value = "/foo")
    public String handleFoo(RedirectAttributes redirectAttrs)
    {
        redirectAttrs.addFlashAttribute("some", "thing");
        return "redirect:/bar";
    }

    @RequestMapping(value = "/bar")
    public void handleBar(@ModelAttribute("some") String some)
    {
        System.out.println("some=" + some);
    }
}

works in Spring MVC 3.2.2

查看更多
Fickle 薄情
3楼-- · 2020-01-26 04:13

For all those like me who were having problems with seeing the POST url in the browser when a validation would fail.

The POST url is a private url that should not be exposed to users but it was automatically rendered when a validation failed. i.e. if a field was below a minimum length. I was using @Valid. I wanted the original GET url of the form to show at all times even when validation bounced back to the form, so I did the following:

        if (validation.hasErrors()) {

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.story", validation);
        redirectAttributes.addFlashAttribute("story", story);
        return new ModelAndView("redirect:/january/2015");

where story is the form object representation, redirectAttributes are RedirectAttributes you put in the method signature and validation is the BindingResult. /january/2015 is the mapping to the GET controller where the form lives.

After this implementation, in the mapping for /january/2015, story comes in intact as follows:

Story story= (Story) model.asMap().get("story");
//story from the POST method

I had to augment my GET method and check if this was not null. If not null, then send this to the form else I would send a newly initialized Story type to the form as default behaviour before.

In this manner, I am able to return to the form with the bindingresults intact (errors show on form) but have my GET url in place of the post url.

查看更多
SAY GOODBYE
4楼-- · 2020-01-26 04:19

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. You can do return return new ModelAndView("redirect:/foo/bar"); in handlePost.

EDIT:

JavaDoc says:

A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a 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);
}

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

查看更多
登录 后发表回答