gpt4 book ai didi

java - RedirectView 后的 Spring Handle FlashAttributes

转载 作者:行者123 更新时间:2023-11-30 07:41:01 26 4
gpt4 key购买 nike

我有一个页面 check,我在其中添加了一个 flashAttribute("error","Test message"); 并将一个 RedirectView 返回到页面登录

@RequestMapping(value = "/login")
public ModelAndView mainPage(Model model){

System.out.println(" - " + model.asMap().get("error") + " - "); //-> - null -

return new ModelAndView("/login");

}

@RequestMapping(value = "/check")
public View checkLogin(RedirectAttributes redirectAttributes,
@RequestHeader(value = "username", required = false)String username,
@RequestHeader(value = "password", required = false)String password){

redirectAttributes.addFlashAttribute("error", "Test message");
return new RedirectView("/login");

}

此行:model.asMap().get("error") 返回 null。我如何处理 RedirectAttributes

最佳答案

我猜 this可能对你有帮助。

@RequestMapping(value = "/login")
public ModelAndView mainPage(RedirectAttributes redirectAttrs){
Map<String> attributesMap = redirectAttrs.getFlashAttributes();

return new ModelAndView("/login");

}

@RequestMapping(value = "/check")
public View checkLogin(RedirectAttributes redirectAttributes,
@RequestHeader(value = "username", required = false)String username,
@RequestHeader(value = "password", required = false)String password){

redirectAttributes.addFlashAttribute("error", "Test message");
return new RedirectView("/login");

}

希望对你有帮助

关于java - RedirectView 后的 Spring Handle FlashAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56631838/

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