SpringMVC @RequestMapping注解應(yīng)用方法示例講解
1、@RequestMapping注解的功能
從注解名稱上我們可以看到,@RequestMapping注解的作用就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。
SpringMVC 接收到指定的請求,就會來找到在映射關(guān)系中對應(yīng)的控制器方法來處理這個(gè)請求。
2、@RequestMapping注解的位置
@RequestMapping標(biāo)識一個(gè)類:設(shè)置映射請求的請求路徑的初始信息
@RequestMapping標(biāo)識一個(gè)方法:設(shè)置映射請求請求路徑的具體信息
@Controller
@RequestMapping("/test")
public class RequestMappingController {
//此時(shí)請求映射所映射的請求的請求路徑為:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
return "success";
}
}3、@RequestMapping注解的value屬性
@RequestMapping注解的value屬性通過請求的請求地址匹配請求映射
@RequestMapping注解的value屬性是一個(gè)字符串類型的數(shù)組,表示該請求映射能夠匹配多個(gè)請求地址所對應(yīng)的請求
@RequestMapping注解的value屬性必須設(shè)置,至少通過請求地址匹配請求映射
<a th:href="@{/testRequestMapping}" rel="external nofollow" >測試@RequestMapping的value屬性-->/testRequestMapping</a><br>
<a th:href="@{/test}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping的value屬性-->/test</a><br>@RequestMapping(
value = {"/testRequestMapping", "/test"}
)
public String testRequestMapping(){
return "success";
}這兩個(gè)地址都匹配同一個(gè)處理
4、@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過請求的請求方式(get或post)匹配請求映射,未設(shè)置則任何方式都能訪問
<a th:href="@{/test}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping的method屬性-->get</a><br>
<form th:action="@{/test}" method="post">
<input type="submit" value="測試@RequestMapping的method屬性--post">
</form>@Controller
public class RequestMappingTestController {
@RequestMapping(value = {"/test" ,"/RequestMappingTest"})
public String success(){
return "success";
}
}@RequestMapping注解的method屬性是一個(gè)RequestMethod類型的數(shù)組,表示該請求映射能夠匹配多種請求方式的請求
@Controller
public class RequestMappingTestController {
@RequestMapping(value = {"/test" ,"/RequestMappingTest"},
method = {RequestMethod.GET,RequestMethod.POST})
public String success(){
return "success";
}
}若當(dāng)前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報(bào)錯(cuò)
405:Request method 'xx' not supported
注:
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部分會講到
5、@RequestMapping注解的params屬性(了解)
@RequestMapping注解的params屬性通過請求的請求參數(shù)匹配請求映射
@RequestMapping注解的params屬性是一個(gè)字符串類型的數(shù)組,可以通過四種表達(dá)式設(shè)置請求參數(shù)和請求映射的匹配關(guān)系
"param":要求請求映射所匹配的請求必須攜帶param請求參數(shù)
"!param":要求請求映射所匹配的請求必須不能攜帶param請求參數(shù)
"param=value":要求請求映射所匹配的請求必須攜帶param請求參數(shù)且param=value
"param!=value":要求請求映射所匹配的請求必須攜帶param請求參數(shù)但是param!=value
<a th:href="@{/test(username='admin',password=123456)" rel="external nofollow" >測試@RequestMapping的params屬性-->/test</a><br>@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username","password!=123456"}
)
public String testRequestMapping(){
return "success";
}注:
若當(dāng)前請求滿足@RequestMapping注解的value和method屬性,但是不滿足params屬性,此時(shí)頁面回報(bào)錯(cuò)400:Parameter conditions "username, password!=123456" not met for actual request parameters: username={admin}, password={123456}
6、@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ò)誤,即資源未找到
7、SpringMVC支持ant風(fēng)格的路徑
?:表示任意的單個(gè)字符
@RequestMapping("/a?a/testAnt")
public String testAnt(){
return "success";
}a1a,a2a......等都可匹配這個(gè)路徑
*:表示任意的0個(gè)或多個(gè)字符
**:表示任意的一層或多層目錄
注意:在使用**時(shí),只能使用/**/xxx的方式
8、SpringMVC支持路徑中的占位符(重點(diǎn))
原始方式:/deleteUser?id=1
rest方式:/deleteUser/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="@{/testRest/1/admin}" rel="external nofollow" >測試路徑中的占位符-->/testRest</a><br>@RequestMapping("/testRest/{id}/{username}")
//使用地址參數(shù)名與方法形參綁定,與順序無關(guān)
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
@RequestMapping("/testRest/{id}/{username}")
//當(dāng)?shù)刂分械膮?shù)名稱與形參名稱一致時(shí)可簡寫,自動尋找綁定,與順序無關(guān)
public String testRest(@PathVariable String id, @PathVariable String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}//最終輸出的內(nèi)容為都是-->id:1,username:admin
到此這篇關(guān)于SpringMVC @RequestMapping注解應(yīng)用方法示例講解的文章就介紹到這了,更多相關(guān)SpringMVC @RequestMapping內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC中@RequestMapping注解的實(shí)現(xiàn)
- SpringMVC中的@RequestMapping注解解析
- 詳解SpringMVC中的@RequestMapping注解
- SpringBoot中的@RequestMapping注解的用法示例
- Spring MVC-@RequestMapping注解詳解
- SpringMVC?@RequestMapping注解屬性詳細(xì)介紹
- 詳解SpringBoot中@PostMapping注解的用法
- SpringBoot中@GetMapping注解的使用
- SpringMVC @GetMapping注解路徑?jīng)_突問題解決
- Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)
相關(guān)文章
SpringBoot一個(gè)非常蛋疼的無法啟動的問題解決
這篇文章主要介紹了SpringBoot一個(gè)非常蛋疼的無法啟動的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談為什么Java中1000==1000為false而100==100為true
這篇文章主要介紹了淺談為什么Java中1000==1000為false而100==100為true,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
純java代碼實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了純java代碼實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng),無連接數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
基于java實(shí)現(xiàn)顏色拾色器并打包成exe
這篇文章主要為大家詳細(xì)介紹了如何基于java編寫一個(gè)簡單的顏色拾色器并打包成exe文件,文中的示例代碼講解詳細(xì),需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
maven profile自動切換環(huán)境參數(shù)的2種方法詳解
這篇文章主要給大家介紹了關(guān)于maven profile自動切換環(huán)境參數(shù)的2種方法,文中通過示例代碼將這兩種方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04

