SpringMVC的注解@RequestMapping屬性及使用
SpringMVC注解@RequestMapping
在之前的 hello world 示例中,用到了 @RequestMapping 注解,它的作用就是將請求和處理請求的控制器方法關聯(lián)起來,建立映射關系。
當 SpringMVC 接受到指定的請求,就會到這個映射關系中找到對應控制器方法來處理這個請求。
一、@RequestMapping 注解的位置
在示例中,注解是用在了方法上,除此之外,還可以用在類上。
1. 作用在方法
@Controller public class RequestMappingController { @RequestMapping("/testRequestMapping") public String testRequestMapping(){ return "success"; } }
此時請求映射所映射的請求的請求路徑為:/testRequestMapping。
2. 作用在類
@Controller @RequestMapping("/test") public class RequestMappingController { //此時請求映射所映射的請求的請求路徑為:/test/testRequestMapping @RequestMapping("/testRequestMapping") public String testRequestMapping(){ return "success"; } }
此時請求映射所映射的請求的請求路徑為:/test/testRequestMapping。
作用在類上以后會經(jīng)常用到,比如有2個模塊:用戶和訂單,那么每個模塊下都會有自己的列表接口 /list。
為了更好的通過名稱區(qū)分出不同模塊,可以給兩個類上加上注解,使其最終路徑為/user/list、/order/list。
當然,你也可以不用類的注解,直接在方法的注解上做區(qū)分,比如/userList和/orderList。
總之,一個請求只能有一個控制器來處理,如果你兩個不同的控制器方法,都使用/list,那么請求過來的時候就不知道該找哪個處理,會報錯。
二、@RequestMapping 注解的 value 屬性
value 屬性通過請求的請求地址匹配請求映射,是必須設置的,否則請求地址匹配不到映射。
另外,value 屬性也是一個字符串類型的數(shù)組,表示該請求映射能夠匹配多個請求地址的請求。
@Controller public class RequestMappingController { @RequestMapping( value = {"/test1", "/test2"} ) public String testRequestMapping(){ return "success"; } }
這里不管請求地址是/test1還是/test2,都可以找到控制器 testRequestMapping(),返回 success.html 。
三、@RequestMapping 注解的 method 屬性
method屬性通過請求的請求方式,比如 get 或 post ,來匹配請求映射。
method 屬性是一個 RequestMethod 類型的數(shù)組,表示該請求映射能夠匹配多種請求方式的請求。
若當前請求的請求地址滿足請求映射的 value 屬性,但是請求方式不滿足 method 屬性,比如控制器是設置是 GET 請求方法,但是請求發(fā)送是 post:
@Controller public class RequestMappingController { @RequestMapping( value = {"/test1"}, method = {RequestMethod.GET} ) public String testRequestMapping(){ return "success"; } }
index.html 添加 post 發(fā)送:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <h1>Hello World</h1> <a th:href="@{/target}" rel="external nofollow" >訪問目標頁面 target.html </a> <br> <a th:href="@{/test1}" rel="external nofollow" >測試@RequestMapping的value屬性-->/test1</a><br> <a th:href="@{/test2}" rel="external nofollow" >測試@RequestMapping的value屬性-->/test2</a><br> <a th:href="@{/test}" rel="external nofollow" >測試@RequestMapping的method屬性-->/test</a><br> <form th:action="@{/test1}" method="post"> <input type="submit"> </form> </body> </html>
點擊 submit 按鈕,瀏覽器報錯405:Request method 'POST' not supported。
如果繼續(xù)添加 post 方法支持,則可以正常訪問:
@Controller public class RequestMappingController { @RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST} ) public String testRequestMapping(){ return "success"; } }
@RequestMapping 結合請求方式的派生注解
對于處理指定請求方式的控制器方法,SpringMVC 中提供了 @RequestMapping 的派生注解。
- @GetMapping:處理 get 請求的映射
- @PostMapping:處理 post 請求的映射
- @PutMapping:處理 put 請求的映射
- @DeleteMapping:處理 delete 請求的映射
使用這種注解,就不需要設置 method 屬性了。
@GetMapping("/test3") public String testGetMapping() { return "success"; }
四、@RequestMapping 注解的 params 屬性
最常用的還是上面說的 value 和 method 屬性,其他的僅做了解。
params 屬性通過請求的請求參數(shù)匹配請求映射,是一個字符串類型的數(shù)組,可以通過四種表達式設置請求參數(shù)和請求映射的匹配關系:
param:要求請求映射所匹配的請求必須攜帶 param 請求參數(shù)
!param:要求請求映射所匹配的請求必須不能攜帶 param 請求參數(shù)
param=value:要求請求映射所匹配的請求必須攜帶 param 請求參數(shù)且 param=value
param!=value:要求請求映射所匹配的請求必須攜帶 param 請求參數(shù)但是 param!=value
舉個例子:
@RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST}, params = {"username", "password!=123456"} ) public String testRequestMapping(){ return "success"; }
這里params = {"username", "password!=123456"}的意思就是,請求中必須帶有參數(shù)username和password,且password參數(shù)的值必須不等于123456。
五、@RequestMapping 注解的 headers 屬性
同樣作個了解。
headers 屬性通過請求的請求頭信息匹配請求映射,是一個字符串類型的數(shù)組,可以通過四種表達式設置請求頭信息和請求映射的匹配關系:
- header:要求請求映射所匹配的請求必須攜帶header請求頭信息
- !header:要求請求映射所匹配的請求必須不能攜帶header請求頭信息
- header=value:要求請求映射所匹配的請求必須攜帶header請求頭信息且header=value
- header!=value:要求請求映射所匹配的請求必須攜帶header請求頭信息且header!=value
若當前請求滿足 @RequestMapping 注解的 value 和 method 屬性,但是不滿足 header 屬性,此時頁面顯示404錯誤,即資源未找到。
@RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST}, params = {"username", "password!=123456"}, headers = {"Host=localhost:8081"} // 這里端口變成8081 ) public String testRequestMapping(){ return "success"; }
我本地端口是 8080,現(xiàn)在去請求頁面,會報錯。
感謝《尚硅谷》的學習資源。
以上就是SpringMVC的注解@RequestMapping屬性及使用的詳細內(nèi)容,更多關于SpringMVC注解@RequestMapping的資料請關注腳本之家其它相關文章!
相關文章
淺談對象數(shù)組或list排序及Collections排序原理
下面小編就為大家?guī)硪黄獪\談對象數(shù)組或list排序及Collections排序原理。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09Spring?Boot中的max-http-header-size配置方式
這篇文章主要介紹了Spring?Boot中的max-http-header-size配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系解析
在我們?nèi)粘5拈_發(fā)中,我們經(jīng)常會用到Filter和Interceptor,這篇文章主要介紹了Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系?,需要的朋友可以參考下2022-10-10postman測試post請求參數(shù)為json類型的實例講解
下面小編就為大家分享一篇postman測試post請求參數(shù)為json類型的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03