您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

使用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
  }
}

PS你可以做回return new ModelAndView("redirect:/foo/bar");handlePost

JavaDoc说:

调用方法时,RedirectAttributes模型为空,除非该方法返回重定向视图名称或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);
}

RedirectAttributes在我的代码中使用RedirectViewmodel.asMap()方法效果很好。

Java 2022/1/1 18:14:46 有410人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶