How do I addObject in Spring RedirectView

前端 未结 1 852
离开以前
离开以前 2020-12-10 20:37

I\'m kinda new to Spring and I\'d like to know how to addOject in RedirectView just like in ModelAndView where I can add objects that can be used in views.

          


        
1条回答
  •  生来不讨喜
    2020-12-10 21:25

    To carry data across a redirect use RedirectAttributes.addFlashAttribute(key, value).

    From the doc:

    A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.

    After the redirect, flash attributes are automatically added to the model of the controller that serves the target URL.

    public ModelAndView processData(
        @Valid final HostFinancialRequest formBean,
        final BindingResult bindingResult, 
        HttpServletRequest request, 
        @RequestParam String passbook,
        RedirectAttributes redirectAttributes) {
        
        // do work an then setup the redirect attributes
        
        redirectAttributes.addFlashAttribute("key", value);
    
        return new ModelAndView("redirect:/credit" + FORM_URL);
    }
    

    0 讨论(0)
提交回复
热议问题