TSattar
Jun 7th, 2008, 08:58 PM
Hi,
I'm trying to get a Model attribute (String) to print out on the view without luck. I'm using an annotated controller and Spring 2.5.
The following handles a post from a form:
@RequestMapping("/addItem.html")
public ModelAndView addItem(
ModelMap model,
@ModelAttribute("command") AddItemCommand cmd, BindingResult result)
throws Exception {
cmd.execute(); //add the item
new AddItemCommandValidator().validate(cmd, result);
ModelAndView mav;
if (result.hasErrors()) {
//need to re-use model to keep validation errors for display in view
mav = new ModelAndView("list.jsp", model); //stay on the page
.addObject("customMsg", "msg_to_be_printed"); //doesn't work
} else {
mav = new ModelAndView(new RedirectView("list.html"))
.addObject("customMsg", "msg_to_be_printed");
}
return mav;
}
If there are errors then I'd like to stay on the page and display errors from the model as well as display a customMsg. If there aren't any errors then a redirect is done.
On the redirect this works fine. However it doesn't work in the former case. Is this the correct way of using it?
I've also tried doing model.addAttribute("customMsg", "msg_to_be_printed") without success.
Any advice appreciated.
Thanks
I'm trying to get a Model attribute (String) to print out on the view without luck. I'm using an annotated controller and Spring 2.5.
The following handles a post from a form:
@RequestMapping("/addItem.html")
public ModelAndView addItem(
ModelMap model,
@ModelAttribute("command") AddItemCommand cmd, BindingResult result)
throws Exception {
cmd.execute(); //add the item
new AddItemCommandValidator().validate(cmd, result);
ModelAndView mav;
if (result.hasErrors()) {
//need to re-use model to keep validation errors for display in view
mav = new ModelAndView("list.jsp", model); //stay on the page
.addObject("customMsg", "msg_to_be_printed"); //doesn't work
} else {
mav = new ModelAndView(new RedirectView("list.html"))
.addObject("customMsg", "msg_to_be_printed");
}
return mav;
}
If there are errors then I'd like to stay on the page and display errors from the model as well as display a customMsg. If there aren't any errors then a redirect is done.
On the redirect this works fine. However it doesn't work in the former case. Is this the correct way of using it?
I've also tried doing model.addAttribute("customMsg", "msg_to_be_printed") without success.
Any advice appreciated.
Thanks