Handler方法的可用参数与返回值
笔记:Pure SpringMVC,对于@Controller中的方法,使用@RequestMapping 的签名为:
1 2 3 4 5 | public (ModelAndView | Map | String | void )
actionName(HttpServletRequest request, HttpServletResponse response,
[,HttpSession]
[,AnyObject]
);
|
要点:
-
当映射的方法返回值为void的时候,则spring mvc框架会生成一个隐含的viewName,根据请求的路径,生成相应的视图名,如果请求的是 xxx.do,则返回的视图为 xxx.jsp ;
-
当映射的方法返回值的类型为String,则方法返回对应的视图名,在结果页面中同样还可以使用方法中的model;
-
当返回的是任意类型的对象时,则框架会将该类型的对象也存放到model中,例如:如果函数返回值是User类型的对象,则框架会自动向Model中设置一个属性:model.addAttribute("user",user) ;
-
如果返回List,则框架会自动向Model中加入:model.addAttribute("userList",userList) ;
-
如果返回Set,则框架会自动向Model中加入:model.addAttribute("userSet",userSet) ;
-
如果返回一个map对象,则框架也会自动向Model中加入:model.addAllAttributes(map) ;
-
也可以直接返回model;而视图名则按默认的策略解析,如果xxx.do解析成xxx.jsp ;
-
更完整的方式是直接返回ModelAndView。
http://www./art/1300156
|