• SpringMVC核心——映射问题


    一、SpringMVC 使用 RequestMapping 来解决映射问题。

    二、在学习 RequestMapping 之前,首先来看一张图。

    这张图表示的是发送一次 http 请求时,所包含的请求 URL,请求 Method,以及请求头和请求体。图中已经标记的很明白了。

    三、RequestMapping

    1.翻译过来是请求映射,在我看来,映射的是 http 请求和处理请求的 handler 方法。

    2.@RequestMapping 可以标注于处理请求的 Handler 类定义处,也可以标注于 handler 方法定义处。

    (1)标注于 Handler 类定义处:提供基础的请求映射信息,相对于 web 应用的根目录。在真实项目中,相当于 namespace 的作用,一般用来划分模块。如:

    请求 URL:http://localhost:8080/springmvc/test/method

    目标Handler:

    /**
     * @author solverpeng
     * @create 2016-08-03-11:06
     */
    @Controller
    @RequestMapping("/test")
    public class RequestMappingTest2 {
        @RequestMapping("/method")
        public String testRequestMapping() {
            System.out.println("test requestMapping..");
            return "success";
        }
    }

    需要注意的是:此时 handler 方法处的 @RequestMapping value 属性相对的是类定义处的路径。

    (2)标注于 handler 方法定义处:提供请求的映射信息,相对于 web 应用的根目录。如:

    请求URL:http://localhost:8080/springmvc/hello

    目标 Handler:

    @RequestMapping("/hello")
    public String hello() {
      System.out.println("hello springmvc!!!");
      return "success";
    }

    3.@RequestMapping 属性详解。

    在对各个属性进行解释说明之前,先来看看 @ReqeustMapping 这个注解类

    从中可以看出,SpringMVC 在 Servlet 环境下,可以对 http 请求的所有信息进行映射,并且通过数组的方式,同个规则下,可以映射多个。

    (1)value 属性

    官方文档是这样解释的:

    The primary mapping expressed by this annotation.
    <p>In a Servlet environment: the path mapping URIs (e.g. "/myPath.do").
    Ant-style path patterns are also supported (e.g. "/myPath/*.do").
    At the method level, relative paths (e.g. "edit.do") are supported
    within the primary mapping expressed at the type level.
    Path mapping URIs may contain placeholders (e.g. "/${connect}")

    解释的已经很明白了,在Servlet环境下,映射 URI,支持 Ant 风格的映射,在方法级别也支持相对路径的映射。映射信息甚至可以从外部文件中获取。

    这里对 Ant 风格进行说明:

    Ant 风格资源地址支持 3 中匹配:

    ?:匹配一个字符

    *:匹配任意个字符

    **:匹配多层路径

    使用在 @RequestMapping 的 value 属性中:

    /test/*/add:匹配 /test/test01/add、/test/test02/add 等URL

    /test/**/add:匹配 /test/springmvc/test01/add、/test/springmvc/test02/add 等 URL

    /test/springmvc/add??:匹配 /test/springmvc/addaa、/test/springmvc/addbb 等 URL

    具体使用:可以映射单个 uri ,也可以映射多个 uri。

    @RequestMapping("/hello")
    public String hello() {
      System.out.println("hello springmvc!!!");
      return "success";
    }
    @RequestMapping(value = {"/testUrl01", "/testUrl02"})
    public String testRequestMappingUrl() {
      System.out.println("test multi url...");
      return "success";
    }

    (2)method 属性

    官方文档是这样解释的:

    The HTTP request methods to map to, narrowing the primary mapping:
    GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
    <p><b>Supported at the type level as well as at the method level!</b>
    When used at the type level, all method-level mappings inherit
    this HTTP method restriction (i.e. the type-level restriction
    gets checked before the handler method is even resolved).
    <p>Supported for Servlet environments as well as Portlet 2.0 environments.

    简单来说,method 属性是对 http 请求方式的一种映射,主要映射 GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE 这几种方式。

    这几种方式以枚举的方式存放在:RequestMethod 枚举类中:

    public enum RequestMethod {
        GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
    }

    支持使用在类级别,也支持使用在方法级别,使用在类级别的时候,所有方法级别的映射都要继承它。

    具体使用:可以映射一个 method,也可以映射多个 method。

    @RequestMapping(value = "/testMultiMethod", method = {RequestMethod.GET, RequestMethod.POST})
    public String testRequestMappingMultiMethod() {
      System.out.println("test multi method...");
      return "success";
    }
    
    @RequestMapping(value = "/testUrl", method = RequestMethod.POST)
    public String testRequestMappingUrlAndMethod(){
      System.out.println("test url and method...");
      return "success";
    }

    (3)params 属性

    官方文档解释内容过多,这里只做说明:

    params 属性映射的是请求参数,看下面几种映射:

    "myParam=myValue" style expressions:请求参数中必须包含 myParam 且值必须为 myValue。

    "myParam!=myValue" style expressions:请求参数中必须包含 myParam 且值必须不为 myValue。

    "!myParam" style expressions:请求参数中必须不能包含 myParam。

    可以使用在类级别,也可以使用在方法级别。使用在方法级别,需要继承自类级别。

    看下面例子:

    @RequestMapping(value = "/testMultiParam", params = {"userName", "age!=23"})
    public String testRequestMappingParam5() {
      System.out.println("test multi params ...");
      return "success";
    }
    
    /**
     * 请求参数中必须包含 userName 但是不能等于jack。
     */
    @RequestMapping(value = "/testParam4", params = "userName!=jack")
    public String testRequestMappingParam4() {
      System.out.println("test param4 ...");
      return "success";
    }
    
    /**
     * 请求参数中不能包含 userName。
     */
    @RequestMapping(value = "/testParam3", params = "!userName")
    public String testRequestMappingParam3() {
      System.out.println("test param3 ...");
      return "success";
    }
    
    /**
     * 请求参数必须为 userName,且值为 jack。否则会返回404错误
     */
    @RequestMapping(value = "/testParam2", params = "userName=jack")
    public String testRequstMappingParam2() {
      System.out.println("test param2 ...");
      return "success";
    }
    
    /**
     * 如果请求参数中没有 userName 则会返回404错误
     */
    @RequestMapping(value = "/testParam", params = "userName")
    public String testRequestMappingParam() {
      System.out.println("test param...");
      return "success";
    }

    (4)header 属性

    header 属性用来映射 http 请求头,主要分为下面几种映射:

    "My-Header=myValue":映射的请求头属性 My-Header 属性值必须为 myValue,否则返回404。

    "My-Header!=myValue":映射的请求头属性 My-Header 属性值必须不为 myValue,否则返回404。

    "!My-Header":映射的请求必须不能存在 My-Header 属性,否则返回404。

    如:

    @RequestMapping(value = "/testHeader", headers = "Accept-Language=zh,zh-CN;q=0.8")
    public String testRequestMappingHeader() {
      System.out.println("test header ...");
      return "success";
    }
    @RequestMapping(value = "/testHeader", headers = "Accept-Language!=zh,zh-CN;q=0.8")
    public String testRequestMappingHeader() {
      System.out.println("test header ...");
      return "success";
    }
    @RequestMapping(value = "/testHeader", headers = "!Accept-Language")
    public String testRequestMappingHeader() {
      System.out.println("test header ...");
      return "success";
    }

    同时支持媒体类型的通配,如:

    RequestMapping(value = "/something", headers = "content-type=text/*")

    将会匹配:"text/html", "text/plain" 等等。

    (5)consumes 属性

    consumes 属性用来映射消费者媒体类型,指的是请求,如:

    consumes = "text/plain"
    consumes = {"text/plain", "application/*"}

    也可以使用 "!" 操作符,如在 "!text/plain" 中,用来过滤 Content-Type 除 "text/plain" 之外的类型。

    Content-Type:请求头的内容类型,表示发送到服务器的内容数据的媒体类型,在服务器端可以通过 request.getContentType() 的方式读取。

    使用 consumes = "text/plain"  表示只对 Content-Type 为 text/plain 的数据进行处理。即(消费请求内容数据

    (6)produces 属性

    produces 属性用来映射生产者媒体类型,指的是响应,如:

    produces = "text/plain"
    produces = {"text/plain", "application/*"}

    也可以使用 "!" 操作符,如在 "!text/plain" 中,用来过滤 Accept 除 "text/plain" 之外的类型。

    在服务器端生产响应头 Content-Type 并指定返回的数据(服务器端是生产者),客户端是消费者。

    总的来说:

    ①客户端—发送请求—服务器:客户端通过请求头Content-Type指定内容体的媒体类型(即客户端此时是生产者),服务器根据Content-Type消费内容体数据(即服务器此时是消费者)。

    ②服务器—发送请求—客户端:服务器生产响应头Content-Type指定的响应体数据(即服务器此时是生产者),客户端根据Content-Type消费内容体数据(即客户端此时是消费者)。

    问题:

    ①服务器端可以通过指定【headers = "Content-Type=application/json"】来声明可处理(可消费)的媒体类型,即只消费Content-Type指定的请求内容体数据。

    ②客户端如何告诉服务器端它只消费什么媒体类型的数据呢?即客户端接受(需要)什么类型的数据呢?服务器应该生产什么类型的数据?此时我们可以请求的Accept请求头来实现这个功能。

    关于这两个属性,具体内容可以参考:

    http://jinnianshilongnian.iteye.com/blog/1695047

    四、总结:

    本节没有解释 springmvc config文件和 web.xml 的配置,就以前一篇文章作为基础。

    SpringMVC 通过 RequestMapping 解决了 http 请求到目标处理方法的映射问题,通过 @RequestMapping 的各个属性,包含了所有请求内容,甚至对特定的类型还分配了独立的属性,如映射消费者、生产者媒体类型。

    在使用的过程中,这种写法非常优雅、灵活。每一个属性都是 String[] 形式,支持同时匹配多个值,属于“”的关系;且不同属性之间同时存在的话,属于“”的关系。在学习该节之前,建议先对 http 请求有完整的理解。

    已经在文章首页对一个 http 请求通过一张图的方式进行了说明。

    还有一些基础内容,可以参看:

    http://www.cnblogs.com/solverpeng/p/5613568.html

    还有关于 url-pattern 的问题,可以参看:

    http://www.cnblogs.com/solverpeng/p/5729763.html

  • 相关阅读:
    小波变换教程(十五)
    小波变换教程(十四)
    小波变换教程(十三)
    小波变换教程(十二)
    小波变换教程(十一)
    【转】小波变换教程(十)
    利用Python进行数据分析学习记录(一)
    【转】小波变换教程(九)
    小波变换教程(八)
    【转】小波变换教程(七)
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5732302.html
Copyright © 2020-2023  润新知