gpt4 book ai didi

java - 如何在 Spring MVC 3.1 中重定向后读取 flash 属性?

转载 作者:IT老高 更新时间:2023-10-28 20:49:29 26 4
gpt4 key购买 nike

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

我有以下代码:

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

}

我错过了什么?

最佳答案

使用Model,它应该有预填充的flash属性:

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

或者,您也可以使用 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
}
}

附:您可以在 handlePost 中返回 return new ModelAndView("redirect:/foo/bar");

编辑:

JavaDoc 说:

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.

它没有提到ModelAndView,所以也许改变handlePost返回"redirect:/foo/bar"字符串或RedirectView:

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

我在我的代码中使用 RedirectAttributesRedirectViewmodel.asMap() 方法,它工作正常。

关于java - 如何在 Spring MVC 3.1 中重定向后读取 flash 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com