Sachin J
Sachin J

Reputation: 2191

difference between Spring 2.5 and Spring 3.1: ModelAndView

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 :)

Upvotes: 4

Views: 3778

Answers (1)

danny.lesnik
danny.lesnik

Reputation: 18639

Spring 3.5 is not released yet I hope you are meaning Spring 3.1.

return ModelAndView is 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

Upvotes: 3

Related Questions