Java @RequestMapping注解功能使用詳解
一、@RequestMapping注解的功能
從注解名稱上我們可以看到,@RequestMapping注解的作用就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。
SpringMVC 接收到指定的請求,就會(huì)來找到在映射關(guān)系中對應(yīng)的控制器方法來處理這個(gè)請求。
二、@RequestMapping注解的位置
@RequestMapping標(biāo)識(shí)一個(gè)類:設(shè)置映射請求的請求路徑的初始信息
@RequestMapping標(biāo)識(shí)一個(gè)方法:設(shè)置映射請求請求路徑的具體信息
@Controller
@RequestMapping("/test")
public class TestRequestMappingController {
//此時(shí)控制器方法所匹配的請求的請求路徑為/test/hello
@RequestMapping("/hello")
public String hello() {
return "success";
}
}三、@RequestMapping注解的value屬性
@RequestMapping注解的value屬性通過請求的請求地址匹配請求映射
@RequestMapping注解的value屬性是一個(gè)字符串類型的數(shù)組,表示該請求映射能夠匹配多個(gè)請求地址 所對應(yīng)的請求
@RequestMapping注解的value屬性必須設(shè)置,至少通過請求地址匹配請求映射
測試映射請求控制器
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時(shí)控制器方法所匹配的請求的請求路徑為/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)識(shí)的位置</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é)果:

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

四、@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過請求的請求方式(get或post)匹配請求映射
@RequestMapping注解的method屬性是一個(gè)RequestMethod類型的數(shù)組,表示該請求映射能夠匹配多種請求方式的請求
若當(dāng)前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報(bào)錯(cuò) 405:Request method 'POST' not supported
注:
除了表單我們默認(rèn)就是post請求,其他的都是get請求
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時(shí)控制器方法所匹配的請求的請求路徑為/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)識(shí)的位置</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)頁面,其他的都報(bào)405錯(cuò)誤-方法不允許。因?yàn)楸韱卧O(shè)置了post請求,我們的@RequestMapping注解的method屬性配置的也是post請求

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時(shí)控制器方法所匹配的請求的請求路徑為/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表單提交時(shí),為method設(shè)置了其他請求方式的字符 串(put或delete),則按照默認(rèn)的請求方式get處理
若要發(fā)送put和delete請求,則需要通過spring提供的過濾器HiddenHttpMethodFilter,在 RESTful部分會(huì)講到
五、@RequestMapping注解的params屬性(了解)
@RequestMapping注解的params屬性通過請求的請求參數(shù)匹配請求映射 @RequestMapping注解的params屬性是一個(gè)字符串類型的數(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ù)不匹配,因?yàn)橐笪覀兊恼埱髤?shù)必須有username。報(bào)400錯(cuò)誤請求-Parameter conditions "username" not met for actual request parameters:
請求參數(shù)必須有username,不能有password,年齡必須為20,性別必須不等于女
@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
//此時(shí)控制器方法所匹配的請求的請求路徑為/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屬性是一個(gè)字符串類型的數(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屬性,此時(shí)頁面 顯示404錯(cuò)誤,即資源未找到
七、SpringMVC支持ant風(fēng)格的路徑
?:表示任意的單個(gè)字符
*:表示任意的0個(gè)或多個(gè)字符
**:表示任意層數(shù)的任意目錄
注意:在使用**時(shí),只能使用/**/xxx的方式
八、SpringMVC支持路徑中的占位符(重點(diǎn))
原始方式:/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)文章
Mybatis Plus查詢時(shí)sql字段名大小寫報(bào)錯(cuò)的解決
這篇文章主要介紹了Mybatis Plus查詢時(shí)sql字段名大小寫報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
java安全?ysoserial?CommonsCollections1示例解析
這篇文章主要介紹了java安全?ysoserial?CommonsCollections1示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程
這篇文章主要介紹了idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Springboot之自定義全局異常處理的實(shí)現(xiàn)
這篇文章主要介紹了Springboot之自定義全局異常處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
java實(shí)現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08

