一、@RequestMapping注解的功能

从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

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

二、@RequestMapping注解的位置

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

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

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
@RequestMapping("/test")
public class RequestMappingController { //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
return "success";
}
}

三、@RequestMapping注解的value属性

@RequestMapping注解的value属性通过请求的请求地址匹配请求映射

@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求

@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
</body>
</html>
package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController { @RequestMapping(value = {"test", "testRequestMapping"})
public String testRequestMapping() {
return "success";
}
}

四、@RequestMapping注解的method属性

@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射

@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错

405:Request method 'POST' not supported

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
</body>
</html>
package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController { @RequestMapping(value = {"test", "testRequestMapping"},
method = RequestMethod.GET)
public String testRequestMapping() {
return "success";
}
}

注:

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

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

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

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

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

2、常用的请求方式有get,post,put,delete

但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符

串(put或delete),则按照默认的请求方式get处理

若要发送put和delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter

五、@RequestMapping注解的params属性

@RequestMapping注解的params属性通过请求的请求参数匹配请求映射

@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系

"param":要求请求映射所匹配的请求必须携带param请求参数

"!param":要求请求映射所匹配的请求必须不能携带param请求参数

"param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value

"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
<a th:href="@{/test(username='admin',password=123456)}">测试@RequestMapping的params属性-->/test</a><br>
</body>
</html>
package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController { @RequestMapping(value = {"test", "testRequestMapping"},
method = RequestMethod.GET,
params = {"username", "password!=123456"})
public String testRequestMapping() {
return "success";
}
}

注:

若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400:

Parameter conditions "username, password!=123456" not met for actual

request parameters: username={admin}, password=

六、@RequestMapping注解的header属性

@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射

@RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系

"header":要求请求映射所匹配的请求必须携带header请求头信息

"!header":要求请求映射所匹配的请求必须不能携带header请求头信息

"header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value

"header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value

若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面显示404错误,即资源未找到

七、SpringMVC支持ant分格的路径

?:表示任意的单个字符

@RequestMapping(value = "/a?a/testAnt")

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

@RequestMapping("/a*a/testAnt")

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

@RequestMapping("/**/testAnt")

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

八、SpringMVC支持路径中的占位符

原始方式:/deleteUser?id=1

rest方式:/deleteUser/1

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
<a th:href="@{/test(username='admin',password=123456)}">测试@RequestMapping的params属性-->/test</a><br>
<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
</body>
</html>
package com.mcode.api.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; /**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username) {
System.out.println("id:" + id + ",username:" + username);
return "success";
}
}

spring-mvc系列:详解@RequestMapping注解(value、method、params、header等)的更多相关文章

  1. 0054 Spring MVC的@Controller和@RequestMapping注解

    @Controller注解 该注解用来指示一个类是一个控制器,在Spring的配置xml文件中开启注解扫描 <context:conponent-scan base-package=" ...

  2. Spring MVC配置详解(3)

    一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...

  3. Spring MVC异常处理详解

    Spring MVC中异常处理的类体系结构 下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要 ...

  4. spring MVC配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  5. Spring mvc 配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. Spring MVC异常处理详解(转)

    下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要实现HandlerExceptionReso ...

  7. spring MVC配置详解(转)

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  8. Spring MVC异常处理详解 ExceptionHandler good

    @ControllerAdvice(basePackageClasses = AcmeController.class) public class AcmeControllerAdvice exten ...

  9. Spring MVC @ModelAttribute详解

    被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...

  10. spring mvc DispatcherServlet详解之前传---FrameworkServlet

    做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationCont ...

随机推荐

  1. 【Redis】Cluster集群

    一.Redis Cluster 工作原理 在引入哨兵机制后,解决了Redis主从架构Master故障时的主从切换问题,保证了Redis服务可用性.但依旧无法解决单机节点出现的写入性能瓶颈(网卡速率.单 ...

  2. React Hooks方法

    1.useState import React, { useState } from "react"; /* 目标: 掌握useState的使用 作用:实现响应式数据的 用法:引入 ...

  3. 2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h

    2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x.y两个小数组长度都是4. 如果有: a + e = b + f = c + g = d + h ...

  4. golang调用sdl2,播放yuv视频

    golang调用sdl2,播放yuv视频 win10 x64下测试成功,其他操作系统下不保证成功. 采用的是syscall方式,不是cgo方式. 见地址 代码如下: package main impo ...

  5. 2021-05-04:给定一个非负整数c,你要判断是否存在两个整数a和b,使得a*a+b*b=c。【举例】c=5时,返回true。c=4时,返回true。c=3时,返回false。

    2021-05-04:给定一个非负整数c,你要判断是否存在两个整数a和b,使得aa+bb=c.[举例]c=5时,返回true.c=4时,返回true.c=3时,返回false. 福大大 答案2021- ...

  6. vue全家桶进阶之路35:Vue3 传递参数query和params

    在 Vue.js 3.x 中,可以通过路由的 params 和 query 属性来传递参数. 通过 params 传递参数 我们可以在路由跳转时通过 params 传递参数.具体方法如下: // 在组 ...

  7. ComboBox1 绑定手动创建的DataTable

    '************************************************** '*过程名称:DT_PAFORMAL '*功能说明:员工类别 '**************** ...

  8. 运行和编译时期资源加载的不同【vue】

    开发语言都有编译和运行两个阶段,很多时候这个也会带来许多bug 如:一个项目在开发阶段测试没有问题,然上线发布后就会有这样那样的问题,譬如说图片的加载,静态数据(js,css,json)读取错误 一 ...

  9. 使用 StarCoder 创建一个编程助手

    如果你是一个软件开发者,你可能已经使用过 ChatGPT 或 GitHub 的 Copilot 去解决一些写代码过程中遇到的问题,比如将代码从一种语言翻译到另一种语言,或者通过自然语言,诸如" ...

  10. ChatGPT 问答

    Win32GUI编程时,创建窗口触发消息的顺序 在Win32 GUI编程中,创建窗口并显示到屏幕上时,系统会触发一系列的消息,这些消息可以用来完成窗口的初始化和其他相关的工作.下面是创建窗口触发消息的 ...