In Spring2.5 we write controller like as follows:
@Controller
public class HelloWorldController{
@RequestMapping(value="/welcome",method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("hello");
model.addObject("msg", "hello world");
return model;
}
}
In Spring 3.1:
@RequestMapping(value="/welcome",method = RequestMethod.GET)
public String printWelcomeString(ModelMap model) {
model.addAttribute("message", "hello world);
return "hello";
}
printwelcomeString() function return a String instead of ModelAndView.
Can any one explain it more?
How it is going to work?
How hello.jsp get model to display?
Thanks 🙂
Spring 3.5 is not released yet I hope you are meaning Spring 3.1.
return ModelAndViewis an old style code writing, used in Spring 2.0. In spring 3.0 add later you can return both ModelAndView or nameView either.I recommend you to return always String (view Name) because some advanced features of Spring MVC will not work properly:
Example:
Spring MVC 3.1 RedirectAttributes is not working