What is the difference between return ModelAndView and return String in Spring MVC? -


i want know different between modelandview , string.

@requestmapping(value="/")     public modelandview mainpage() {         return new modelandview("home"); } 

and second piece of code returning string:

@requestmapping(value="/")     public string mainpage() {         return "home"; } 

you can return many things controller, , spring automatically try deduce there.

when return modelandview information needs passed controller embedded inside it. no suprise there.

if return string, assume name of view use. wrap in modelandview object string view , existing model embedded well.

spring lot of 'magic' on return types, on parameter types. allows write code in style more intuitive you, i.e. following 2 examples same:

@requestmapping(value="/") public modelandview mainpage() {     model m = new model();     m.put("key", "value");     return new modelandview(m,"main"); } 

and

@requestmapping(value="/") public string mainpage(model m) {     m.put("key", "value");     return "main"; } 

this second variant little less verbose , easier test separately. model passed in empty model (unless forwarded other controller).


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -