我有以下代码。我想在nextPage.jsp上访问booleanValueObj。这是怎么做到的?对于每个请求,对象并不总是可用于nextPage()方法,因此requestParam似乎不合适。
@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;
}
}
答案 0 :(得分:3)
您无法将booleanValueObj传递给重定向的页面。如果booleanValueObj只是一个布尔值,那么通过请求参数传递给/ nextPageClass似乎是合适的。
@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;
}
}
答案 1 :(得分:2)
对于Spring 3.x,这有效:
@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
}
}