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

次に、ビューにフラッシュ メッセージを表示します。

 <p>${flashSuccessMsg}</p>

しかし、フラッシュ メッセージのリストを取得してから、ビューでこれを反復処理したいと思います。

これは可能ですか?

フラッシュメッセージに名前を付けていない場合redirectAttributes.addFlashAttribute("Update Successful"); 、ビューでそれを取得するにはどうすればよいですか?

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 に答える