1. 程式人生 > 其它 >使用重定向後拿到訊息的方法

使用重定向後拿到訊息的方法

技術標籤:springbootjavajavascriptspring

使用redirect重定向url後可以用RedirectAttributes拿到訊息並將訊息展示到前端

通過attributes.addFlashAttribute方法
    @PostMapping("/types")
    public String posy(Type type, RedirectAttributes attributes){
        Type type1 = typeService.saveType(type);
        if (type1 == null
) { attributes.addFlashAttribute("messgage","操作失敗"); } else { attributes.addFlashAttribute("message","操作成功"); } return "redirect:/admin/types"; }