SpringMVC常用参数绑定注解(annnotation)

@RequestParam

用于获取Http请求中的参数,来自于Query String.即SevletRequest#getParameter

@PathVariable

用于获取URI占位符传入的值,一般用在RESTful接口上

@RequestBody

将请求消息体绑定到一个对象上

@ModelAttribute

此注解可以放置于参数上,也可以放置于方法上;

// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
    return accountManager.findAccount(number);
}

// Add multiple attributes
@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
    model.addAttribute(accountManager.findAccount(number));
    // add more ...
}
往Model中添加属性值,cotroller中每个方法执行之前都会执行这个方法(含有@ModelAttribute注解的方法)

而将ModelAttribute放置于参数上时,表示从Model中获取指定名称的参数;

@SessionAttribute  从spring4.3.x开始有

表示从global session中获取指定名称的参数,并绑定;

@RequestAttribute

表示从Request请求返回内获取指定名称的参数,并绑定

@CookieValue

表示从Http请求 cookie中获取指定名称的参数,并绑定;

其他RESTful相关注解

@Controller 

@RestController 

@RequestMapping 

@ResponseBody

@ExceptionHandler 

@ControllerAdvice

@RestControllerAdvice

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值