Java @RequestMapping注解功能使用詳解
一、@RequestMapping注解的功能
從注解名稱上我們可以看到,@RequestMapping注解的作用就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。
SpringMVC 接收到指定的請求,就會來找到在映射關(guān)系中對應(yīng)的控制器方法來處理這個請求。
二、@RequestMapping注解的位置
@RequestMapping標(biāo)識一個類:設(shè)置映射請求的請求路徑的初始信息
@RequestMapping標(biāo)識一個方法:設(shè)置映射請求請求路徑的具體信息
@Controller
@RequestMapping("/test")
public class TestRequestMappingController {
//此時控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping("/hello")
public String hello() {
return "success";
}
}三、@RequestMapping注解的value屬性
@RequestMapping注解的value屬性通過請求的請求地址匹配請求映射
@RequestMapping注解的value屬性是一個字符串類型的數(shù)組,表示該請求映射能夠匹配多個請求地址 所對應(yīng)的請求
@RequestMapping注解的value屬性必須設(shè)置,至少通過請求地址匹配請求映射
測試映射請求控制器
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping({"/hello","/abc"})
public String hello() {
return "success";
}
}index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymelef.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h1>index.html</h1>
<a th:href="@{/hello}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping注解所標(biāo)識的位置</a>
<a th:href="@{/abc}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping注解的value屬性</a>
</body>
</html>success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymelef.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h1>success.html</h1>
</body>
</html>結(jié)果:

點超鏈接跳轉(zhuǎn)到下面頁面

四、@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過請求的請求方式(get或post)匹配請求映射
@RequestMapping注解的method屬性是一個RequestMethod類型的數(shù)組,表示該請求映射能夠匹配多種請求方式的請求
若當(dāng)前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報錯 405:Request method 'POST' not supported
注:
除了表單我們默認(rèn)就是post請求,其他的都是get請求
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping(
value = {"/hello","/abc"},
method = RequestMethod.POST)
public String hello() {
return "success";
}
} <h1>index.html</h1>
<a th:href="@{/hello}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping注解所標(biāo)識的位置</a>
<a th:href="@{/abc}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping注解的value屬性</a>
<form th:action="@{/hello}" method="post">
<input type="submit" value="測試@RequestMapping注解的method屬性">
</form>結(jié)果:

除了按鈕可以跳轉(zhuǎn)頁面,其他的都報405錯誤-方法不允許。因為表單設(shè)置了post請求,我們的@RequestMapping注解的method屬性配置的也是post請求

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping(
value = {"/hello","/abc"},
method = {RequestMethod.POST,RequestMethod.GET})
public String hello() {
return "success";
}
}上面這種方式也可以匹配兩種請求,不過默認(rèn)的情況下也是匹配兩種請求
注:
1、對于處理指定請求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
處理get請求的映射-->@GetMapping
處理post請求的映射-->@PostMapping
處理put請求的映射-->@PutMapping
處理delete請求的映射-->@DeleteMapping
2、常用的請求方式有g(shù)et,post,put,delete
但是目前瀏覽器只支持get和post,若在form表單提交時,為method設(shè)置了其他請求方式的字符 串(put或delete),則按照默認(rèn)的請求方式get處理
若要發(fā)送put和delete請求,則需要通過spring提供的過濾器HiddenHttpMethodFilter,在 RESTful部分會講到
五、@RequestMapping注解的params屬性(了解)
@RequestMapping注解的params屬性通過請求的請求參數(shù)匹配請求映射 @RequestMapping注解的params屬性是一個字符串類型的數(shù)組,可以通過四種表達(dá)式設(shè)置請求參數(shù)和請求映射的匹配關(guān)系
params可以使用四種表達(dá)式:
"param":要求請求映射所匹配的請求必須攜帶param請求參數(shù)
"!param":要求請求映射所匹配的請求必須不能攜帶param請求參數(shù)
"param=value":要求請求映射所匹配的請求必須攜帶param請求參數(shù)且param=value "param!=value":要求請求映射所匹配的請求必須攜帶param請求參數(shù)但是param!=value
路徑匹配,請求方式也匹配,但是請求參數(shù)不匹配,因為要求我們的請求參數(shù)必須有username。報400錯誤請求-Parameter conditions "username" not met for actual request parameters:
請求參數(shù)必須有username,不能有password,年齡必須為20,性別必須不等于女
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping(
value = {"/hello","/abc"},
method = {RequestMethod.POST,RequestMethod.GET},
params = {"username","!password","age=20","gender!=女"}
)
public String hello() {
return "success";
}
} <a th:href="@{/hello?username=admin}" rel="external nofollow" >測試@RequestMapping注解的params屬性</a><br>
<a th:href="@{/hello(username='admin')}" rel="external nofollow" >測試@RequestMapping注解的params屬性</a><br>六、@RequestMapping注解的headers屬性(了解)
@RequestMapping注解的headers屬性通過請求的請求頭信息匹配請求映射 @RequestMapping注解的headers屬性是一個字符串類型的數(shù)組,可以通過四種表達(dá)式設(shè)置請求頭信 息和請求映射的匹配關(guān)系
"header":要求請求映射所匹配的請求必須攜帶header請求頭信息
"!header":要求請求映射所匹配的請求必須不能攜帶header請求頭信息
"header=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header=value
"header!=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header!=value 若當(dāng)前請求滿足@RequestMapping注解的value和method屬性,但是不滿足headers屬性,此時頁面 顯示404錯誤,即資源未找到
七、SpringMVC支持ant風(fēng)格的路徑
?:表示任意的單個字符
*:表示任意的0個或多個字符
**:表示任意層數(shù)的任意目錄
注意:在使用**時,只能使用/**/xxx的方式
八、SpringMVC支持路徑中的占位符(重點)
原始方式:/deleteUser?id=1
rest方式:/user/delete/1
SpringMVC路徑中的占位符常用于RESTful風(fēng)格中,當(dāng)請求路徑中將某些數(shù)據(jù)通過路徑的方式傳輸?shù)椒?wù)器中,就可以在相應(yīng)的@RequestMapping注解的value屬性中通過占位符{xxx}表示傳輸?shù)臄?shù)據(jù),在通過@PathVariable注解,將占位符所表示的數(shù)據(jù)賦值給控制器方法的形參
<a th:href="@{test/rest/admin/1}" rel="external nofollow" >測試@RequestMapping注解的value屬性中的占位符</a> @RequestMapping("/test/username/{username}/{id}")
public String testRest(@PathVariable("id") Integer id,@PathVariable("username") String username) {
System.out.println("id:" + id + ",username:" + username);
return "success";
}
}到此這篇關(guān)于Java @RequestMapping注解功能使用詳解的文章就介紹到這了,更多相關(guān)Java @RequestMapping內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java語言Consistent Hash算法學(xué)習(xí)筆記(代碼示例)
這篇文章主要介紹了Java語言Consistent Hash算法學(xué)習(xí)筆記(代碼示例),分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解
這篇文章主要介紹了SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
idea復(fù)制module(項目)并在一個窗口展示的教程詳解
這篇文章主要介紹了idea復(fù)制module(項目)并在一個窗口展示的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Java并發(fā)之條件阻塞Condition的應(yīng)用代碼示例
這篇文章主要介紹了Java并發(fā)之條件阻塞Condition的應(yīng)用代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
Java中的UrlDecoder 和 UrlEncoder_動力節(jié)點Java學(xué)院整理
HTML 格式編碼的實用工具類。該類包含了將 String 轉(zhuǎn)換為 application/x-www-form-urlencoded MIME 格式的靜態(tài)方法。下文通過實例代碼給大家介紹Java中的UrlDecoder 和 UrlEncoder知識,感興趣的的朋友一起看看吧2017-07-07

