Difference between Spring 2.5 and Spring 3.1: ModelAndView

In Spring2.5, we write the controller 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";

}
Function

printwelcomeString () returns a string instead of ModelAndView.

Can anyone explain this more? How will it work? How do hello.jsp get the model to display? Thank:)

+3
source share
1 answer

Spring 3.5 is not yet released. I hope you mean Spring 3.1.

return ModelAndViewis the old style code entry used in Spring 2.0. In Spring 3.0 add later you can return both ModelAndView or nameView.

I recommend that you always return String (view Name), because some additional Spring MVC functions will not work properly:

: Spring MVC 3.1 RedirectAttributes

+2

All Articles