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.
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.
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.
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.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With