@RequestMapping注解的源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default ""; @AliasFor("path")
String[] value() default {}; @AliasFor("value")
String[] path() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {};
}

@RequestMapping注解的功能

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

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

该注解可以标识在类和方法上。

@RequestMapping注解的位置

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

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

如果有请求路径的初始信息,则先设置请求路径的初始信息才能设置请求路径的具体信息。

比如,想从index的超链接跳转到welcome页面:

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestMapping}">访问welcome页面 </a>
</body>
</html>

index的超链接跳转的路径是/testRequestMapping

welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
<h1>Welcome to My Blog</h1>
</body>
</html>

welcome的控制器的类和方法上都加上RequestMapping注解

@Controller
@RequestMapping("/hello")
public class RequestMappingController { @RequestMapping("/testRequestMapping")
public String welcome(){
return "welcome";
}
}

结果就是跳转失败

如果修改跳转路径为/hello/testRequestMapping,即可成功

<a th:href="@{/hello/testRequestMapping}">访问welcome页面 </a>

@RequestMapping注解的value属性

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

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

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

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestMapping}">通过/testRequestMapping访问welcome页面 </a>
<br>
<a th:href="@{/test}">通过/test访问welcome页面 </a>
</body>
</html>
@Controller
public class RequestMappingController { @RequestMapping(value = {"/testRequestMapping","/test"})
public String welcome(){
return "welcome";
}
}

两个超连接都可访问成功

@RequestMapping注解的method属性

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

@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求

若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method 'POST' not supported

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<form th:action="@{/test}" method="post">
<input type="submit" value="以post方式提交">
</form>
<br>
<form th:action="@{/test}" method="get">
<input type="submit" value="以get方式提交">
</form>
</body>
</html>

welcome.html的控制器

    @RequestMapping(value = "/test",
method = {RequestMethod.GET})
public String welcome(){
return "welcome";
}

如果不设置控制器的method,则indexpostget两种方式都可以访问welcome.html

如果设置控制器的methodRequestMethod.GET,则只能用get方式访问

post方式访问:

以get方式访问:

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

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

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

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

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

免去了设置method的麻烦,只写映射路径即可

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

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

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

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<form th:action="@{/test}" method="put">
<input type="submit" value="以put方式提交">
</form>
<br>
</body>
</html>
    @PostMapping("/test")
public String welcome(){
return "welcome";
}

@RequestMapping注解的params属性

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

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

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

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

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

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

<a th:href="@{/test(username='admin',password=123456)}">测试@RequestMapping的params属性-->/test</a><br>
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username","password!=123456"}
)
public String testRequestMapping(){
return "welcome";
}

@RequestMapping注解的headers属性

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

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

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

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

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

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

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

SpringMVC路径的模糊匹配

:表示任意的单个字符

    @RequestMapping(value = "/a?a/test")
public String welcome(){
return "welcome";
}

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

    @RequestMapping(value = "/a*a/test")
public String welcome(){
return "welcome";
}

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

    @RequestMapping(value = "/**/test")
public String welcome(){
return "welcome";
}
  • 注意:

    • 在使用/**时,只能使用/**/xxx的方式

    • ?*不可以用 / ?来占位

SpringMVC支持路径中的占位符(重点)

原始方式:/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}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
//最终输出的内容为-->id:1,username:admin

【SpringMVC】@RequestMapping注解的更多相关文章

  1. SpringMVC RequestMapping注解

    1.@RequestMapping 除了修饰方法,还可以修饰类 2.类定义处:提供初步的请求映射信息.相对于WEB应用的根目录  方法处:提供进一步细分映射信息  相对于类定义处的URL.若类定义处未 ...

  2. SpringMVC @RequestMapping注解详解

    @RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...

  3. 超详细 SpringMVC @RequestMapping 注解使用技巧

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  4. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo

    使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中. RequestMappingInfo封装了PatternsRequestCondition, ...

  5. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系

    一般我们开发时,使用最多的还是@RequestMapping注解方式. @RequestMapping(value = "/", param = "role=guest& ...

  6. SpringMVC使用注解@RequestMapping映射请求

    pringMVC通过使用@RequestMapping注解,实现指定控制器可以处理哪些URL请求. 控制器的类定义及方法定义处都可以标注@RequestMapping: 类定义处:提供初步的请求映射信 ...

  7. (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解

    http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...

  8. SpringMVC详解一、@RequestMapping注解与Controller接收参数

    SpringMVC详解一.@RequestMapping注解与Controller接收参数 https://blog.csdn.net/mxcsdn/article/details/80719258 ...

  9. SpringMVC源码解读 - RequestMapping注解实现解读

    SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系  https://www.cnblogs.com/leftthen/p/520840 ...

随机推荐

  1. Java基础00-分支语句6

    1. 流程控制 1.1 流程控制语句概述 1.2 流程控制语句分类 1.3 顺序结构 2. if语句 2.1 if语句格式1 适合一种情况的判断 执行流程图: 2.2 if语句格式2 适合两种情况的判 ...

  2. Day10 类与对象-面向对象编程(1)

    面向对象编程(OOP) 面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据. 抽象 三大特征: 封装 继承 多态 从认识论角度考虑是先有对象后有类.对象,是具体的事物.类,是抽象的, ...

  3. JavaScript学习笔记:你必须要懂的原生JS(二)

    11.如何正确地判断this?箭头函数的this是什么? this是 JavaScript 语言的一个关键字.它是函数运行时,在函数体内部自动生成的一个对象,只能在函数体内部使用. this的绑定规则 ...

  4. debian 9安装细节

    1.安装KDE桌面 2.开机桌面正常启动,首先在grub启动界面,按"e"键,在linux......quiet后面加上nomodeset,然后进入桌面,在终端输入: su -vi ...

  5. js里的发布订阅模式及vue里的事件订阅实现

    发布订阅模式(观察者模式) 发布订阅模式的定义:它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. 发布订阅模式在JS中最常见的就是DOM的事件绑定与触发 ...

  6. GC垃圾回收机制详解

    JVM堆相关知识    为什么先说JVM堆?  JVM的堆是Java对象的活动空间,程序中的类的对象从中分配空间,其存储着正在运行着的应用程序用到的所有对象.这些对象的建立方式就是那些new一类的操作 ...

  7. RegisterClass/RegisterClassEx -- 注册窗口类

    (1)函数原型 1 //RegisterClass 2 ATOM RegisterClass( 3 const WNDCLASS * lpWndClass //Long pointer to a WN ...

  8. 插入排序(insertion_sort)——Python实现

      # 插入排序 # 作用:对给出的n个顺序不定的数进行排序 # 输入:任意数组A # 输出:按顺序排列的数组A # 时间复杂度 n(n-1) 至 (n(n-1))/2   # 插入排序过程 # 第一 ...

  9. JVM参数模版

    首先需要自己根据机器的配置设置JVM中各区域的初步大小,如下: -Xms4096M-Xmx4096M-Xmn3072M-Xss1M-XX:MetaspaceSize=256M-XX:MaxMetasp ...

  10. 漫谈CUDA优化

    ​ 作者:Lawliet 翻译:仿佛若有光 前言: 几个月前,我根据 Simoncelli 2016 年的论文编写了自己的自动编码器,用于研究目的.一开始,我想使用一些流行的深度学习框架(例如 Ten ...