SpringMVC RequestMapping注解详解

来源:互联网 发布:ios的写作软件 编辑:程序博客网 时间:2024/05/10 08:22

@RequestMapping注解的作用是对用户的请求映射到指定的控制器或方法,所以该注解可以用来修饰类和方法,在RequestMapping的源码中看到这么一句话

@Target({ElementType.METHOD, ElementType.TYPE})

也同样可以说明该注解所修饰的类型。

RequestMapping修改方法

在RequestMapping修饰方法时,可以有如下四个参数

  1. value(默认)
  2. method
  3. params
  4. heads

其中,value为默认参数,表示用户请求的url,如果只用到该参数,则不需要写参数名,如

@RequestMapping("/helloworld")

method用来过滤用户请求的方法,即post或get,如

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

params为请求携带的参数,该项可包括多个参数,参数之间为“与”

@RequestMapping(value = "/helloworld",params = "username,password")

也可以用数组的形式表示

@RequestMapping(value = "/helloworld",params = {"username=admin", "password"})

heads为http请求头中携带的内容,如Accept-Encoding等,具体方法同params

未完。。。

0 0
原创粉丝点击