RquestMapping的注解功能、注解使用范围、注解的属性详情

一、@RequestMapping注解的注解功能

@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

二、@RequestMapping注解使用范围

可以用来表示方法

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/requestMapping")
public class RequestMappingController {
    @RequestMapping("/success")
    public String index(){
        return "success";
    }
}

没有标识类时路径为:

http://localhost:8080/项目名称/方法的的RquestMapping的value

有标识类时路径为:

http://localhost:8080/项目名称/类的RquestMapping的value/方法的的RquestMapping的value

这样做的好处可以避免不同的控制器,同名的方法相冲突,比如:

类A与类B都有lsit,如果不标明类的RquestMapping的value,会报错,系统不知道应该访问A的list还是B的list。

三、@RequestMapping注解的属性

1. value:设置访问的地址,参数为String[]。

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = {"/requestMapping","/request"})//可以有多个,访问时只需要满足其中一个即可
public class RequestMappingController {
    @RequestMapping("/success")
    public String index(){
        return "success";
    }
}

2. method:设置请求的类型,默认为都接收。

类型(常用有四个):
GET,
HEAD,
POST,
PUT,
PATCH,
DELETE,
OPTIONS,
TRACE;

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
//可以有多个value和method
@RequestMapping(value = {"/requestMapping", "/request"}, method = {RequestMethod.PUT, RequestMethod.GET})
public class RequestMappingController {
    @RequestMapping("/success")
    public String index() {
        return "success";
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }
}

post与get的区别:

  1. Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示。
  2. Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的限制,只能传递大约1024字节.
  3. Post顾名思义,就是为了将数据传送到服务器端,Get就是为了从服务器段取得数据.而Get之所以也能传送数据,只是用来设计告诉服务器,你到底需要什么样的数据。Post的信息作为http请求的内容,而Get是在Http头部传输的。

注:

对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解

处理get请求的映射–>@GetMapping

处理post请求的映射–>@PostMapping

处理put请求的映射–>@PutMapping

处理delete请求的映射–>@DeleteMapping

但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符串(put或delete),则按照默认的请求方式get处理。

3. params:请求参数,根据参数匹配请求

@RequestMapping(
            value = {"/testParam"},
            method = {RequestMethod.POST},
            //一定要有username,不能有password,一定要有email,且必须等于123@qq.com,一定要有sex,且不能等于男
            params = {"username", "!password", "email=123@qq.com", "sex!=男",}
    )
    public String testParam() {
        return "success";
    }

4. headers:请求头信息,根据参数匹配请求,用法与params一致。

@RequestMapping(
            value = {"/testHeaders"},
            //一定要有Host,且必须为 localhost:8081,如果匹配不成功则报404
            headers = {"Host=localhost:8081"}
    )
    public String testHeaders() {
        return "success";
    }
  • Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9
  • Accept-Encoding: gzip, deflate, br
  • Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
  • Cache-Control: max-age=0
  • Connection: keep-alive
  • Content-Length: 60
  • Content-Type: application/x-www-form-urlencoded
  • Cookie: Webstorm-47ce7496=9cb61066-595e-42e7-8ca3-59706fabb93b; Idea-98cc9d10=d042c95a-dc73-44d8-b776-7a08b8bd31ae
  • Host: localhost:8080
  • Origin: http://localhost:8080
  • Referer: http://localhost:8080/springMVC_demo2/
  • sec-ch-ua: " Not;A Brand";v=“99”, “MicrosoftEdge”;v=“103”, “Chromium”;v=“103”
  • sec-ch-ua-mobile: ?0
  • sec-ch-ua-platform: “Windows”
  • Sec-Fetch-Dest: document
  • Sec-Fetch-Mode: navigate
  • Sec-Fetch-Site: same-origin
  • Sec-Fetch-User: ?1
  • Upgrade-Insecure-Requests: 1
  • User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62

5. 支持ant风格的路径

  • ?:表示任意的单个字符

  • *:表示任意的0个或多个字符

  • *:表示任意的一层或多层目录

注意:在使用**时,只能使用/**/xxx的方式

<a th:href="@{/request/a1a/testAnt1}">?测试</a><br>
<a th:href="@{/request/a11111a/testAnt2}">*测试</a><br>
<a th:href="@{/request/a/a/a/testAnt3}">**测试</a><br>
@GetMapping("/a?a/testAnt1")
    public String testAnt1(){
        return "success";
    }
    @GetMapping("/a*a/testAnt2")
    public String testAnt2(){
        return "success";
    }
    @GetMapping("/a/**/a/testAnt3")
    public String testAnt3(){
        return "success";
    }

6. 支持路径中的占位符(restful风格)

原始方式:/deleteUser?id=1

rest方式:/deleteUser/1

SpringMVC路径中的占位符常用于restful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,在通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参

<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")//如果占位符中有这个value,请求中必须对应,否则报404
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
    System.out.println("id:"+id+",username:"+username);
    return "success";
}
//最终输出的内容为-->id:1,username:admin
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值