Spring MVC中的多个flash消息

Mar*_*ark 3 spring-mvc

在Spring MVC 3.1中我可以做到:

@RequestMapping(value = "{id}/edit", method = RequestMethod.POST)
public String update(Category category, @PathVariable Integer id, 
    @RequestParam("childrenOrder") int[] childrenOrder,
    RedirectAttributes redirectAttributes) {

    if (!id.equals(category.getCategoryId())) throw new IllegalArgumentException("Attempting to update the wrong category");
    categoryMapper.updateByPrimaryKey(category);
    redirectAttributes.addFlashAttribute("flashSuccessMsg", "Update Successful");  //ADD FLASH MESSAGE
    return "redirect:/admin/categories.html";
}
Run Code Online (Sandbox Code Playgroud)

然后在视图中显示flash消息:

 <p>${flashSuccessMsg}</p>
Run Code Online (Sandbox Code Playgroud)

但我宁愿有一个flash消息列表,然后在视图中迭代它.

这可能吗?

如果我这样做:redirectAttributes.addFlashAttribute("Update Successful"); 即我没有命名flash消息,我该如何在视图中检索它?

jel*_*ies 8

您是否尝试过使用RedirectAttributes addFlashAttribute(String attributeName,Object attributeValue)

@RequestMapping(value = "{id}/edit", method = RequestMethod.POST)
public String update(Category category, @PathVariable Integer id, @RequestParam("childrenOrder") int[] childrenOrder, RedirectAttributes redirectAttributes) {
    if (!id.equals(category.getCategoryId())) throw new IllegalArgumentException("Attempting to update the wrong category");
    categoryMapper.updateByPrimaryKey(category);

    List<String> messages = new ArrayList<String>();
    // populate messages 

    redirectAttributes.addFlashAttribute("messages", messages);  

    return "redirect:/admin/categories.html";
}
Run Code Online (Sandbox Code Playgroud)

稍后,在您的视图中,您可以messages使用<c:foreach />标记进行迭代:

<c:foreach items="${messages}">
...
</c:foreach>
Run Code Online (Sandbox Code Playgroud)