返回ModelAndView的方式
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list(
@UserAuth UserAuth user,
ModelAndView mav) {
if (!user.isAuthenticated()) {
mav.setViewName("redirect:http://www.test.com/login.jsp");
return mav;
}
mav.setViewName("list");
mav.addObject("articles", listService.getLists());
return mav;
}
Run Code Online (Sandbox Code Playgroud)
返回String的方式
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(
@UserAuth UserAuth user,
Model model) {
if (!user.isAuthenticated()) {
return "redirect:http://www.test.com/login.jsp";
}
model.addAttribute("articles", listService.getLists());
return "list";
}
Run Code Online (Sandbox Code Playgroud)
这些工作相同.哪个更好?有什么区别?
这听起来对专家来说可能是愚蠢的,Spring但我不得不问.你如何决定何时使用ModelAndView对比Model?
毕竟我研究过的最好的答案就是这个.它已经提到,ModelAndView是一个古老的道路,Model与String返回是一种新的方式Spring.
我的问题是,我们ModelAndView现在应该弃用旧的Model吗?或者是否有任何需要使用ModelAndView它的情况.
此外,没有人知道为什么要改ModelAndView到Model和String值View,和有什么好处?