Redirect using model object in spring

I need to pass an object to the page I'm going to redirect

@RequestMapping(value = "/createNewQuest",method={ RequestMethod.GET, RequestMethod.POST })
    public ModelAndView createNewQuest(Quest quest,ModelAndView mav) {
        questService.addQuest(quest);


        mav.addObject("quest", quest);
        mav.setViewName("redirect:/control/displayQuest");
        return mav;
    }

my controller class looks like this, but the displayQuest page does not receive the quest object?

any help would be very noticeable.

+3
source share
2 answers

Spring added Flash attributes to handle this scenario:

FlashMap . @RequestMapping RedirectAttributes flash . Flash, RedirectAttributes, "" FlashMap. "" FlashMap , URL.

    @RequestMapping(value = "/createNewQuest",method={ RequestMethod.GET, RequestMethod.POST })
    public ModelAndView createNewQuest(@ModelAttribute("quest") Quest quest,
         BindingResult binding, ModelAndView mav, 
             final RedirectAttributes redirectAttributes) {

        questService.addQuest(quest);

        redirectAttributes.addFlashAttribute("quest", quest);
        mav.setViewName("redirect:/control/displayQuest");
        return mav;
    }
+2

spring,

<form:form name="yourformname" id="formid" commandName="quest">

"" spring formName,

0

All Articles