@RequestMapping 参数说明

  value:定义处理方法的请求的 URL 地址。(重点)

  method:定义处理方法的 http method 类型,如 GET、POST 等。(重点)

  params:定义请求的 URL 中必须包含的参数。或者不包含某些参数。(了解)

  headers:定义请求中 Request Headers 必须包含的参数。或者不包含某些参数。(了解)

@RequestMapping 的用法

  @RequestMapping 有两种标注方式,一种是标注在类级别上,一种是标注在方法级别上。标注在方法上时,value 表示访问该方法的 URL 地址。标注在类上时,value 相当于一个命名空间,即访问该 Controller 下的任意方法都需要带上这个命名空间。例如:

Java代码

@Controller
@RequestMapping("/example")
public class ExampleController { @RequestMapping
public String execute(){
return "example_page";
} @RequestMapping("/todo")
public String doSomething(){
return "example_todo_page";
} }

  1:/example.action:执行的是 execute() 方法。execute() 方法的 @RequestMapping 注解缺省 value 值,在这种情况下,当访问命名空间时默认执行的是这个方法。方法级别上的 @RequestMapping 标注是必须的,否则方法无法被正确访问。

  2:/example/todo.action执行的是 doSomething() 方法。类级别上的 @RequestMapping 标注不是必须的,在不写的情况下,方法上定义的 URL 都是绝对地址,否则,方法上定义的 URL 都是相对于它所在的 Controller 的。

@RequestMapping(method):指定页面请求方式

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(){
return "example_register_page";
}

  method 的值一旦指定,那么,处理方法就只对指定的 http method 类型的请求进行处理。 这里方法/register只能使用get请求,使用post请求无法访问

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register1(){
return "example_register_get_page";
} @RequestMapping(value = "/register", method = RequestMethod.POST)
public String register2(){
return "example_register_post_page";
}

  可以为多个方法映射相同的 URI,不同的 http method 类型,Spring MVC 根据请求的 method 类型是可以区分开这些方法的。当 /example/register.action 是以 GET 的方式提交的时候,Spring MVC 调用 register1() 来处理请求;若是以 POST 的方式提交,则调 register2() 来处理提交的请求。

  method 若是缺省没指定,并不是说它默认只处理 GET 方式的请求,而是它可以处理任何方式的 http method 类型的请求。指定 method 是为了细化映射 ( 缩小处理方法的映射范围 ),在 method 没有指定的情况下,它的映射范围是最大的。

@RequestMapping(params)

  与 method 相类似,作用是为了细化映射。只有当 URL 中包含与 params 值相匹配的参数的请求,处理方法才会被调用。

@RequestMapping(value = "/find", params = "target")
public String find1(){
return "example_find1_page";
} @RequestMapping(value = "/find", params = "!target")
public String find2(){
return "example_find2_page";
} @RequestMapping(value = "/search", params = "target=product")
public String search1(){
return "example_search1_page";
} @RequestMapping(value = "/search", params = "target!=product")
public String search2(){
return "example_search2_page";
}

  find1():请求的 URL 中必须要有 target 参数,才能够到达此方法。如 /example/find.action?target 或 /example/find.action?target=x 等

  find2():请求的 URL 中必须不能有 target 参数,才能够到达此方法。如 /example/find.action 或 /example/find.action?q=x 等

  search1():请求的 URL 中必须要有 target=product 参数,才能够到达此方法。如 /example/search.action?target=product 等

  search2():请求的 URL 中必须不能有 target=product 参数,才能够到达此方法。如 /example/search.action?target=article 等

@RequestMapping(headers)

  headers 的作用也是用于细化映射。只有当请求的 Request Headers 中包含与 heanders 值相匹配的参数,处理方法才会被调用。

@RequestMapping(value = "/specify", headers = "accept=text/*")
public String specify(){
return "example_specify_page";
}

  请求的 Request Headers 中 Accept 的值必须匹配 text/* ( 如 text/html ),方法才会被调用。

@RequestMapping支持Ant风格的通配符

通配符 说明 示例
? 匹配一个任意字符 /a/?b 可以匹配/a/ab;/a/cb。但不能匹配/a/acb之类
* 匹配任意长度的字符 /a/ *b可以匹配/a/cb;/a/acb。但不能匹配/a/cb/vb
** 匹配多层路径 可以匹配/a/ab;/a/acb;/a/ab/abc/…/…

本文转自:http://www.cnblogs.com/caoyc/p/5635173.html

SpringMVC @RequestMapping注解详解的更多相关文章

  1. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  2. SpringMVC @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 http://blog.csdn.net/walkerjong/article/details/7994326

  3. springMVC的注解详解

    springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...

  4. springmvc常用注解详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  5. Spring MVC @RequestMapping注解详解(2)

    @RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...

  6. @RequestMapping注解详解

    @RequestMapping是一个用来处理请求地址映射的注解,可用于类或者方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestMapping注解有六个属性,下面进行 ...

  7. Spring MVC @RequestMapping注解详解

    @RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...

  8. @ModelAttribute注解详解

    @ModelAttribute注解详解 1.@ModelAttribute定义: 被该注解定义的方法,会在该方法所在的controller的任何目标方法执行之前执行 2.@ModelAttribute ...

  9. @PathVariable @RequestParam @RequestBody等参数绑定注解详解

    一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解:   @PathVariable;(这里指ur ...

随机推荐

  1. linux 代码更新-打包-重启脚本

    #! /bin/sh base=/home/project/myblog cd $base git pull ] then echo "Error in git pull!!! Stop d ...

  2. Selenium 2自动化测试实战5(模块调用)

    一.模块调用 1.创建一个目录project,并且在目录下面创建两个文件 project/ 一 pub.py L一 count.py 在pub.py文件中创建add函数. #pub.py def ad ...

  3. 关于struts2防止表单重复提交

    struts2防表单重复提交有两种方式. 其一是action的重定向,跳转时设置type为从一个action跳转到另一个action或者另一个页面, 使用户提交后,所停留的位置,不是当前处理数据的Ac ...

  4. P3367 【模板】并查集

    喵呜~~(题面) 这题其实很早就过了,但是呢,以前过的时候真的基本上是CtrlC+CtrlV,这次把代码重新码了一遍,对并查集也有了一个基本清晰的认识 #include<iostream> ...

  5. value(C# )

    上下文关键字 value 用在普通属性声明的 set 访问器中. 此关键字类似于方法的输入参数. 关键字 value 引用客户端代码尝试分配给属性的值. 在以下示例中,MyDerivedClass 有 ...

  6. oracle truncate表 恢复操作

    truncate恢复表 1.创建测试用表 conn elan/elan create table haha as select * from dba_users; 2.查询表数据 ) from hah ...

  7. Java学习开发第二阶段总结

    第二阶段的学习总结: 在这次学习中虽说任务量是比上次提升了不少.但大部分的内容都于C语言相同或者类似.学习起来相对来说很轻松.但也在这次学习中学到新的知识 ①Jshell 在cmd中运行Jshell脚 ...

  8. spring boot-11.全局捕获异常

    1.在Spring boot 中如果发生错误,浏览器访问会默认跳转到Whitelabel Error Page 这个错误页面,如果是客户端访问的话返回JSON格式的错误数据,说明spring boot ...

  9. 高性能异步分布式事务TCC框架(资料汇总)

    https://github.com/yu199195/hmily tcc源码解析系列(一)之项目结构 https://yu199195.github.io/2017/10/11/TCC/tcc-on ...

  10. Almost Sorted Array(o(nlgn)求解LIS)

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...