Spring MVC Controllers Return Type

后端 未结 6 1914
别跟我提以往
别跟我提以往 2020-12-23 11:39

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

@RequestMapping(value=\"/owners/{ownerId}\", method=RequestMethod.GET)
pub         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 12:24

    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).

提交回复
热议问题