• spring mvc -@RequestMapping注解详解


    https://www.cnblogs.com/caoyc/p/5635173.html

    @RequestMapping参数说明:

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

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

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

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

    @RequestMapping的用法:

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

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

    1、/example.action:执行的是excute()方法。excute()方法的@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";
    }
    register

    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";
    }
    View Code

    可以为多个方法映射相同的 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";
    }
    View Code

      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";
    }
    View Code

    请求的 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/…/…
  • 相关阅读:
    Hexo博客系列(二)-在多台机器上利用Hexo发布博客
    Hexo博客系列(一)-Windows系统配置Hexo v3.x个人博客环境
    [原创]VMware Workstation 14.1.3 Pro安装CentOS_7.6.1810
    [原创]前后端交互的方式整理
    [转载]白素贞的身世之谜
    [原创]存储过程里面的递归
    [原创]SpringBoot上传图片踩的坑
    [原创]markdown语法学习(commonmark)
    使用IntelliJ IDEA 前最好修改的配置
    软件开发资源下载
  • 原文地址:https://www.cnblogs.com/arrows/p/10521588.html
Copyright © 2020-2023  润新知