简体   繁体   中英

How do you redirect to another URI and access an object from the previous modelAndView

I have the following code. I want to access booleanValueObj on nextPage.jsp. How is this done? The object is not always available to nextPage() method on every request, so a requestParam seems like it's not appropriate.

@RequestMapping(method=RequestMethod.POST)
public ModelAndView sendEmail()
{
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject(booleanValueObj, true);
    modelAndView.setViewName("redirect: /nextPageClass");
    return modelAndView;
}

@RequestMapping("/nextPageClass")
public class NextPageController 
{
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView nextPage() 
    {
        ModelAndView modelAndView = new ModelAndView("/nextPage");
        return modelAndView;
    }
}

You can't pass booleanValueObj to a redirected page. If booleanValueObj is simply a boolean value, it seems appropriate to be passed to /nextPageClass thru the request parameters.

@RequestMapping(method=RequestMethod.POST)
public ModelAndView sendEmail(HttpServletResponse resp)
{
    resp.sendRedirect("/nextPageClass?booleanValueObj=true");
    return null;
}

@RequestMapping("/nextPageClass")
public class NextPageController 
{
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView nextPage(HttpServletRequest req) 
    {
        ModelAndView modelAndView = new ModelAndView("/nextPage");

        Boolean booleanValueObj = null;
        String booleanValueParam = req.getParameter("booleanValueObj");
        if (booleanValueParam != null)
             booleanValueObj = Boolean.parse(booleanValueParse);
        modelAndView.addObject("booleanValueObj", booleanValueObj);

        return modelAndView;
    }
}

For Spring 3.x this works:

@RequestMapping(value = "/item/delete.htm", method = RequestMethod.GET)
public ModelAndView deleteItem(@RequestParam("id") String id) {
    if (isOk(id)) {
       itemService.delete(id);
       return new ModelAndView("/item/list");
    } else {
       return new ModelAndView("redirect:/item/error.htm");  // <== here redirect
    }
}       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM