@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,則index
的post
和get
兩種方式都可以訪問welcome.html
如果設置控制器的method
為RequestMethod.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