Java @RequestMapping注解功能使用詳解
一、@RequestMapping注解的功能
從注解名稱上我們可以看到,@RequestMapping注解的作用就是將請求和處理請求的控制器方法關聯(lián)起來,建立映射關系。
SpringMVC 接收到指定的請求,就會來找到在映射關系中對應的控制器方法來處理這個請求。
二、@RequestMapping注解的位置
@RequestMapping標識一個類:設置映射請求的請求路徑的初始信息
@RequestMapping標識一個方法:設置映射請求請求路徑的具體信息
@Controller @RequestMapping("/test") public class TestRequestMappingController { //此時控制器方法所匹配的請求的請求路徑為/test/hello @RequestMapping("/hello") public String hello() { return "success"; } }
三、@RequestMapping注解的value屬性
@RequestMapping注解的value屬性通過請求的請求地址匹配請求映射
@RequestMapping注解的value屬性是一個字符串類型的數組,表示該請求映射能夠匹配多個請求地址 所對應的請求
@RequestMapping注解的value屬性必須設置,至少通過請求地址匹配請求映射
測試映射請求控制器
@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注解所標識的位置</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>
結果:
點超鏈接跳轉到下面頁面
四、@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過請求的請求方式(get或post)匹配請求映射
@RequestMapping注解的method屬性是一個RequestMethod類型的數組,表示該請求映射能夠匹配多種請求方式的請求
若當前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報錯 405:Request method 'POST' not supported
注:
除了表單我們默認就是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注解所標識的位置</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>
結果:
除了按鈕可以跳轉頁面,其他的都報405錯誤-方法不允許。因為表單設置了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"; } }
上面這種方式也可以匹配兩種請求,不過默認的情況下也是匹配兩種請求
注:
1、對于處理指定請求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
處理get請求的映射-->@GetMapping
處理post請求的映射-->@PostMapping
處理put請求的映射-->@PutMapping
處理delete請求的映射-->@DeleteMapping
2、常用的請求方式有get,post,put,delete
但是目前瀏覽器只支持get和post,若在form表單提交時,為method設置了其他請求方式的字符 串(put或delete),則按照默認的請求方式get處理
若要發(fā)送put和delete請求,則需要通過spring提供的過濾器HiddenHttpMethodFilter,在 RESTful部分會講到
五、@RequestMapping注解的params屬性(了解)
@RequestMapping注解的params屬性通過請求的請求參數匹配請求映射 @RequestMapping注解的params屬性是一個字符串類型的數組,可以通過四種表達式設置請求參數和請求映射的匹配關系
params可以使用四種表達式:
"param":要求請求映射所匹配的請求必須攜帶param請求參數
"!param":要求請求映射所匹配的請求必須不能攜帶param請求參數
"param=value":要求請求映射所匹配的請求必須攜帶param請求參數且param=value "param!=value":要求請求映射所匹配的請求必須攜帶param請求參數但是param!=value
路徑匹配,請求方式也匹配,但是請求參數不匹配,因為要求我們的請求參數必須有username。報400錯誤請求-Parameter conditions "username" not met for actual request parameters:
請求參數必須有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屬性是一個字符串類型的數組,可以通過四種表達式設置請求頭信 息和請求映射的匹配關系
"header":要求請求映射所匹配的請求必須攜帶header請求頭信息
"!header":要求請求映射所匹配的請求必須不能攜帶header請求頭信息
"header=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header=value
"header!=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header!=value 若當前請求滿足@RequestMapping注解的value和method屬性,但是不滿足headers屬性,此時頁面 顯示404錯誤,即資源未找到
七、SpringMVC支持ant風格的路徑
?:表示任意的單個字符
*:表示任意的0個或多個字符
**:表示任意層數的任意目錄
注意:在使用**時,只能使用/**/xxx的方式
八、SpringMVC支持路徑中的占位符(重點)
原始方式:/deleteUser?id=1
rest方式:/user/delete/1
SpringMVC路徑中的占位符常用于RESTful風格中,當請求路徑中將某些數據通過路徑的方式傳輸到服務器中,就可以在相應的@RequestMapping注解的value屬性中通過占位符{xxx}表示傳輸的數據,在通過@PathVariable注解,將占位符所表示的數據賦值給控制器方法的形參
<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"; } }
到此這篇關于Java @RequestMapping注解功能使用詳解的文章就介紹到這了,更多相關Java @RequestMapping內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java語言Consistent Hash算法學習筆記(代碼示例)
這篇文章主要介紹了Java語言Consistent Hash算法學習筆記(代碼示例),分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Java并發(fā)之條件阻塞Condition的應用代碼示例
這篇文章主要介紹了Java并發(fā)之條件阻塞Condition的應用代碼示例,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Java中的UrlDecoder 和 UrlEncoder_動力節(jié)點Java學院整理
HTML 格式編碼的實用工具類。該類包含了將 String 轉換為 application/x-www-form-urlencoded MIME 格式的靜態(tài)方法。下文通過實例代碼給大家介紹Java中的UrlDecoder 和 UrlEncoder知識,感興趣的的朋友一起看看吧2017-07-07