@RequestMapping

来源:互联网 发布:php image not loaded 编辑:程序博客网 时间:2024/05/27 14:12

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

1.1.1  URL路径映射

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

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

 

1.1.2  窄化请求映射

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

 

如下:

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

@Controller

@RequestMapping("/item")

 

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

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

@RequestMapping("/queryItem ")

 

访问地址为:/item/queryItem

 

 

1.1.3  请求方法限定

u  限定GET方法

@RequestMapping(method = RequestMethod.GET)

 

如果通过Post访问则报错:

HTTP Status 405 - Request method 'POST' not supported

 

例如:

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

u  限定POST方法

 

@RequestMapping(method = RequestMethod.POST)

 

如果通过Post访问则报错:

HTTP Status 405 - Request method 'GET' not supported

 

u  GET和POST都可以

@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
0 0
原创粉丝点击