SpringBoot問題:RedirectAttributes存值后讀取不到


首先,檢查Controller上面是@Controller還是@RestController(兩者區別自行百度)

其次,如下

    @GetMapping("/redirect")
    public String redirect(RedirectAttributes redirectAttributes)
    {
        redirectAttributes.addFlashAttribute("test", 1);
        return "redirect:/show";
    }

    @GetMapping("/show")
    @ResponseBody
    //必須要添加@ModelAttribute標簽,否側將讀不到值
    //且必須指定變量名,並不會自動做匹配
    public Map<String, Object> show(@ModelAttribute("test") int test)
    {
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("String", test);
        return modelMap;
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM