spring小结

[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,此时会自动绑定
  @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的属性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值