重定向后FlashAttributes不在模型中

时间:2018-05-04 12:57:32

标签: spring spring-mvc

我有这种控制器方法:

@GetMapping("/notations")
public String listAll(Model model) {
    Iterable<PriceNotation> allItems = loadAllNotations();
    model.addAttribute("notations", allItems);
    return "supply/notations";
}

然后我有这个重定向到上面的方法:

@GetMapping(value = "/notations/delete")
public String delete(@RequestParam(name="id", required=true) Long id, RedirectAttributes redirectAttributes)
{
    try {
        notationRepository.deleteById(id);
    } catch (RuntimeException e) {
        redirectAttributes.addFlashAttribute("message", "delete failed");
    }
    return "redirect:/notations";
}

当我在重定向后在第一个方法中放置断点时,模型为空。虽然documentation说:

  

重定向后,flash属性会自动添加到   提供目标URL的控制器模型。

同样在我的html页面中,我有这个标题,它应该显示消息:

<h2 th:text="${message}"></h2>

这个标题也是空的。我错过了什么?

PS,我知道之前已经问过这个问题,但没有接受答案,也没有一个建议对我有用。

1 个答案:

答案 0 :(得分:0)

它们不会添加到模型中,因为它们作为查询参数传递,如example.com?message=abc

所以你可以:

  • 使用@RequestParam在控制器中访问它们,然后添加到您的模型
  • 或者使用${#param.message[0]}
  • 在百里香中访问它们

总之,您应该将redirectAttributes视为接收控制器(listAll)中的reqular查询参数。