springmvc中@RequestMapping的使用

  通过RequestMapping注解可以定义不同的处理器映射规则。

1.1 URL路径映射

  @RequestMapping(value="/item")或@RequestMapping("/item")。

  value的秩是数组,可以将多个url映射到同一个方法。

1.2 窄化请求映射

  在class上添加@RequestMapping(url)指定通用请求前缀,限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。 

  如下:

  @RequestMapping放在类名上边,设置请求前缀 

    @Controller

    @RequestMapping("/item")

  方法名上边设置请求映射url

    @RequestMapping放在方法名上边,如下:

      @RequestMapping("/queryItem ")

  访问地址为:/item/queryItem

 1.3 请求方法限定

  •  限定GET方法

    @RequestMapping(method = RequestMethod.GET)

    如果通过Post访问则报错:

      HTTP Status 405 - Request method 'POST' not supported

    例如:

    @RequestMapping(value="/editItem",method=RequestMethod.GET)

  • 限定POST方法

    @RequestMapping(method = RequestMethod.POST)

     如果通过Post访问则报错:

      HTTP Status 405 - Request method 'GET' not supported

  • GETPOST都可以

    @RequestMapping(method={RequestMethod.GET,RequestMethod.POST})

转载于:https://www.cnblogs.com/wyhluckdog/p/10200246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值