[TOC] ### 一、@RequestMapping註解的功能 從註解名稱上我們可以看到,@RequestMapping註解的作用就是將請求和處理請求的控制器方法關聯起來,建立映射關係。 SpringMVC 接收到指定的請求,就會來找到在映射關係中對應的控制器方法來處理這個請求。 ### 二、@R ...
目錄
- 一、@RequestMapping註解的功能
- 二、@RequestMapping註解的位置
- 三、@RequestMapping註解的value屬性
- 四、@RequestMapping註解的method屬性
- 五、@RequestMapping註解的params屬性
- 六、@RequestMapping註解的header屬性
- 七、SpringMVC支持ant分格的路徑
- 八、SpringMVC支持路徑中的占位符
一、@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";
}
}