Spring MVC Controllers Return Type

It’s the same logic but it’s not the same version of spring.

The ModelAndView object is the spring 2.x way of handling model and views.
In the example you gave, the modelandview object will load the “helloWorld” view (depending on your templating engine could be helloWorld.jsp, or helloWorld.html, …) with one data “message” in the model.

The other way is the spring 3.x way. You could have wrote exactly the same example as your helloworld.

@RequestMapping(value="/helloWorld", method=RequestMethod.GET)
public String helloWorld(Model model) {
    model.addAttribute("message", "Hello World!");
    return "helloWorld";
}

The model is automaticly populated at request.

And we can simplify this notation as the url mapping “helloWorld” is directly the view name.

@RequestMapping(value="/helloWorld", method=RequestMethod.GET)
public void helloWorld(Model model) {
     model.addAttribute("message", "Hello World!");
}

the helloWorld view will be automaticly loaded

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)