@RequestMapping注解的属性的使用及具体细节详解

@RequestMapping注解的属性的使用及具体细节详解

1.@RequestMapping注解标识的位置

@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息

2.@RequestMapping注解的value属性

作用:通过请求的请求路径匹配请求

-value属性是数组类型,即当前浏览器所发送的请求路径匹配value属性中任何一个值,则当前请求就会被注解所标识的方法进行处理

3.@RequestMapping注解的method属性

作用:通过请求的请求方式匹配请求

-method属性是RequestMethod类型的数组,即当前浏览器所发送的请求请求方式匹配value属性中任何一个则当前请求就会被注解所标识的方法进行处理

若浏览器发送请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配,此时页面报错:405 - Request method ‘xxx’ not supported

在@RequestMapping的基础上,结合请求方式的一些派生注解:
@GetMapping,@PostMapping,@DeleteMapping,@PutMapping

4.@RequestMapping注解的params属性

作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满@params属性的设置
params可以使用四种表达式:“param”;“!param”;“param=value”;“param!=val

5.@RequestMapping注解的headers属性

作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息满足headers属性的设置 (用法与params属性的用法一样)

6.SpringMVC支持ant风格的路径

在@RequestMapping注解的value属性值中设置一些特殊字符
?:任意的单个字符
*:表示任意的0个或多个字符
:表示任意层数的任意目录(注意使用方式只能将写在双斜线中,前后不能有任何其他字符)

7.@RequestMapping注解使用路径中的占位符

传统方式:/deleteUser?id=1
rest:/user/delete/1
需要在@RequestMapping注解的value属性所设置的路径中,使用{xxx}的方式表示路径中的数据再通过@PathVariable注解将占位符所标识的值和控制器方法的形参进行绑定

以下是实现的代码:

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>index.html</h1>
    <a th:href="@{/hello}">测试@RequestMapping注解标识的位置</a><br>
    <a th:href="@{/abc}">测试@RequestMapping注解的value属性</a><br>
    <form th:action="@{/hello}" method="post">
        <input type="submit" value="测试@RequestMapping注解的method属性">
    </form>
    <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的value属性</a><br>
    <a th:href="@{/hello(username=admin)}">测试@RequestMapping注解的value属性</a><br>
    <a th:href="@{/aaa/test/ant}">测试@RequestMapping注解支持ant风格的路径</a><br>

    <a th:href="@{/test/rest/admin/1}">测试@RequestMapping的value属性中的占位符</a><br>
</body>
</html>

success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>success.html</h1>
</body>
</html>
package com.qcw.controller;

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

@Controller
public class PortalController {

    @RequestMapping("/")
    public String portal(){
        return "index";
    }
}
package com.qcw.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {

    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping(value = {"/hello","/abc"},
            method = {RequestMethod.POST,RequestMethod.GET},
            //params = {"username","!password","age=20","gender!=女"}
            headers = {"referer"}
    )
    public String hello(){
        return "success";
    }

    @RequestMapping("/**/test/ant")
    public String testAnt(){
        return "success";
    }


    @RequestMapping("/test/rest/{username}/{id}")
    public String testRest(@PathVariable("id") Integer id,@PathVariable("username") String username){
        System.out.println("id = " + id +","+"username = "+username);
        return "success";
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值