zoukankan      html  css  js  c++  java
  • spring mvc(2):请求地址映射(@RequestMapping)

    @RequestMapping 参数说明

    value
    定义处理方法的请求的 URL 地址。
    method
    定义处理方法的 http method 类型,如 GET、POST 等。
    params
    定义请求的 URL 中必须包含的参数。
    headers
    定义请求中 Request Headers 必须包含的参数。

    @RequestMapping 的用法

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

    ExampleController.java
    @Controller
    @RequestMapping("/example")
    public class ExampleController {

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

    /example.action
    执行的是 execute() 方法。execute() 方法的 @RequestMapping 注解缺省 value 值,在这种情况下,当访问命名空间时默认执行的是这个
    方法。方法级别上的 @RequestMapping 标注是必须的,否则方法无法被正确访问。
    /example/todo.action
    执行的是 doSomething() 方法。类级别上的 @RequestMapping 标注不是必须的,在不写的情况下,方法上定义的 URL 都是绝对地址,否则,
    方法上定义的 URL 都是相对于它所在的 Controller 的。

    @RequestMapping(method)

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

    method 的值一旦指定,那么,处理方法就只对指定的 http method 类型的请求进行处理。 

    @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 的值
    @RequestMapping("/enter")
    public String enter(){
        return "example_enter_page";
    }

    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 ),方法才会被调用。

  • 相关阅读:
    codeforces C. No to Palindromes!
    codeforces D. Pashmak and Parmida's problem
    codeforces C. Little Pony and Expected Maximum
    codeforces D. Count Good Substrings
    codeforces C. Jzzhu and Chocolate
    codeforces C. DZY Loves Sequences
    codeforces D. Multiplication Table
    codeforces C. Painting Fence
    hdu 5067 Harry And Dig Machine
    POJ 1159 Palindrome
  • 原文地址:https://www.cnblogs.com/llq5/p/5181555.html
Copyright © 2011-2022 走看看