gpt4 book ai didi

spring-mvc - Spring MVC 中的多条 flash 消息

转载 作者:行者123 更新时间:2023-12-04 06:34:43 26 4
gpt4 key购买 nike

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

然后在 View 中显示闪现消息:

 <p>${flashSuccessMsg}</p>

但我宁愿有一个 Flash 消息列表,然后在 View 中对其进行迭代。

这可能吗?

如果我这样做:redirectAttributes.addFlashAttribute("更新成功");即我没有命名 Flash 消息,然后如何在 View 中检索它?

最佳答案

您是否尝试过使用 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>

关于spring-mvc - Spring MVC 中的多条 flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com