Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Controllers Return Type

I've seen examples where a controller returns a String (which indicates the view)

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) {   Owner owner = ownerService.findOwner(ownerId);     model.addAttribute("owner", owner);     return "displayOwner" } 

And I also see examples where a controller returns a "ModelAndView" object type

public ModelAndView helloWorld() {     ModelAndView mav = new ModelAndView();     mav.setViewName("helloWorld");     mav.addObject("message", "Hello World!");     return mav; } 

What is the difference between the two and which should I use? Cause either way I can get my view resolved.

like image 406
Girish Dusane Avatar asked Sep 12 '11 05:09

Girish Dusane


People also ask

What is the return type of spring controller?

In Spring MVC, the below return types are possible. 8. When method is annotated with @ResponseBody, the return type is written directly to the response HTTP body.

What is the return type in MVC?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.

Is it necessary to use ResponseEntity as a return type only in the controller layer?

Your controller method should only return ResponseEntity<Success> . It will not be responsible for returning error or exception responses. You will implement a class that handles exceptions for all controllers.


2 Answers

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

like image 92
Thomas Avatar answered Oct 11 '22 14:10

Thomas


If we are talking about MVC 3, than, both are correct. But directly returning ModelAndView is the old way, and more verbal.

If you are returning just a string (without @ResponseBody which is something else), this string is treated as view name, and spring pushes it to view resolvers - so, you dont have to worry (at least, while you are writing controllers), what type of view renderer you'll use (let it be jsp or velocity, it doesn't matter). You only propagate the Model instance, and returnes a hint what to do with it next. Proper ModelAndView object is made later internally by string.

Generally, spring 3 gives you more flexibility with arguments and return types (see Defining @RequestMapping handler methods section in Spring documentaton).

like image 32
Adam Jurczyk Avatar answered Oct 11 '22 14:10

Adam Jurczyk