3

在 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";
}

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

 <p>${flashSuccessMsg}</p>

但我宁愿有一个闪存消息列表,然后在视图中对其进行迭代。

这可能吗?

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

4

1 回答 1

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

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

<c:foreach items="${messages}">
...
</c:foreach>
于 2012-08-09T11:52:13.680 回答