[size=large]1.@RequestMapping 方法的返回类型 [/size]
@RequestMapping 方法返回的类型对象用于决定应该调用哪个view,并需要装载的model
或显示指明两者(如modelandview对象)或显示其他一个,另外一个隐式指定(如Model, View),另外一种情况是返回类型为void, 这表明handler方法内会处理返回的content,比如使用方法参数response直接写数据给客户端
以下是一些可能的返回类型
1)ModelAndView
2)Model,
3)A Map object for exposing a model
4)View
5)String 指定view name
6)void if the method handles the response itself
7)If the method is annotated with @ResponseBody,
the return type is written to the response HTTP body
[size=large]2.model自动绑定的问题[/size]
当返请求类型是[color=red]get[/color]时,即使方法的参数中没有Model,此时会自动绑定
以上代码等同于
当返请求类型是[color=red]post[/color]时,必须绑定Model,否则在页面上引用不到user的属性
@RequestMapping 方法返回的类型对象用于决定应该调用哪个view,并需要装载的model
或显示指明两者(如modelandview对象)或显示其他一个,另外一个隐式指定(如Model, View),另外一种情况是返回类型为void, 这表明handler方法内会处理返回的content,比如使用方法参数response直接写数据给客户端
以下是一些可能的返回类型
1)ModelAndView
2)Model,
3)A Map object for exposing a model
4)View
5)String 指定view name
6)void if the method handles the response itself
7)If the method is annotated with @ResponseBody,
the return type is written to the response HTTP body
[size=large]2.model自动绑定的问题[/size]
当返请求类型是[color=red]get[/color]时,即使方法的参数中没有Model,此时会自动绑定
@RequestMapping(value="/contact/new", method=RequestMethod.GET)
public String displayPublicIndex(User user) {
user.setAge(20);
user.setName("zhangsan");
return "/contact/new";
}
以上代码等同于
@RequestMapping(value="/contact/new", method=RequestMethod.GET)
public String displayPublicIndex(User user,Model model) {
user.setAge(20);
user.setName("zhangsan");
model.addAttribute("user",user); return "/contact/new";
}
当返请求类型是[color=red]post[/color]时,必须绑定Model,否则在页面上引用不到user的属性