一文詳解SpringMVC中的@RequestMapping注解
@RequestMapping注解的常用屬性包括:
value:用于指定URL路徑,可以是單個(gè)路徑字符串,或者是路徑字符串?dāng)?shù)組。
method:用于指定請求方法,可以是RequestMethod枚舉值,或者是RequestMethod數(shù)組。
params:用于指定請求的參數(shù)條件,可以是參數(shù)名或參數(shù)名值對的字符串?dāng)?shù)組。
headers:用于指定請求頭條件,可以是請求頭的名稱和值的字符串?dāng)?shù)組。
@Controller @RequestMapping("/user") public class UserController { @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestParam("username") String username, @RequestParam("password") String password) { // 處理登錄請求 return "success"; } @RequestMapping(value = "/profile", method = RequestMethod.GET, params = "user=123") public String userProfile() { // 返回用戶資料頁面 return "profile"; } }
上述示例中,@RequestMapping用于控制器類上表示該類處理以"/user"開頭的所有請求。@RequestMapping用于處理方法上,其中value=“/login"表示處理”/user/login"路徑的POST請求,method=RequestMethod.POST表示只處理POST請求方法。@RequestParam注解用于獲取請求參數(shù)。
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"; }
4、@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過請求的請求方式(get或post)匹配請求映射
@RequestMapping注解的method屬性是一個(gè)RequestMethod類型的數(shù)組,表示該請求映射能夠匹配多種請求方式的請求
若當(dāng)前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報(bào)錯(cuò)405:Request method ‘POST’ not supported
<a th:href="@{/test}" rel="external nofollow" rel="external nofollow" >測試@RequestMapping的value屬性-->/test</a><br> <form th:action="@{/test}" method="post"> <input type="submit"> </form>
@RequestMapping( value = {"/testRequestMapping", "/test"}, method = {RequestMethod.GET, RequestMethod.POST} ) public String testRequestMapping(){ return "success"; }
注:
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è)字符
*:表示任意的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}") public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){ System.out.println("id:"+id+",username:"+username); return "success"; } //最終輸出的內(nèi)容為-->id:1,username:admin
以上就是一文詳解SpringMVC中的@RequestMapping注解的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC@RequestMapping注解的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JAVA開發(fā)常用類庫UUID、Optional、ThreadLocal、TimerTask、Base64使用方法與實(shí)例詳
這篇文章主要介紹了JAVA開發(fā)常用類庫UUID、Optional、ThreadLocal、TimerTask、Base64使用方法與實(shí)例詳解,需要的朋友可以參考下2020-02-02軟件開發(fā)基礎(chǔ)之設(shè)計(jì)模式概述
這篇文章介紹了軟件開發(fā)基礎(chǔ)之設(shè)計(jì)模式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法
這篇文章主要介紹了詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08第三方包jintellitype實(shí)現(xiàn)Java設(shè)置全局熱鍵
本文主要介紹了,在java中使用第三方插件包jintellitype來實(shí)現(xiàn)全局熱鍵,非常的簡單,但是很實(shí)用,有需要的朋友可以參考下,歡迎一起來參與改進(jìn)此項(xiàng)目2014-09-09線程池之newCachedThreadPool可緩存線程池的實(shí)例
這篇文章主要介紹了線程池之newCachedThreadPool可緩存線程池的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06