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 ModelAn开发者_如何学PythondView();
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
}
}
精彩评论