SpringMVC中的@RequestMapping注解解析
1. RequestMapping 概念
SpringMVC使用@RequestMapping注解為控制器指定可以處理哪些 URL 請求。
在控制器的類定義及方法定義處都可標(biāo)注 @RequestMapping。
① 標(biāo)記在類上:提供初步的請求映射信息。相對于 WEB 應(yīng)用的根目錄
② 標(biāo)記在方法上:提供進(jìn)一步的細(xì)分映射信息。相對于標(biāo)記在類上的 URL。
若類上未標(biāo)注 @RequestMapping,則方法處標(biāo)記的 URL 相對于 WEB 應(yīng)用的根目錄 。
作用:DispatcherServlet 截獲請求后,就通過控制器上 @RequestMapping 提供的映射信息確定請求所 對應(yīng)的處理方法。
2. RequestMapping源碼
package org.springframework.web.bind.annotation; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String[] value() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; }
參數(shù)說明
- value:定義處理方法的請求的 URL 地址。(重點(diǎn))
- method:定義處理方法的 http method 類型,如 GET、POST 等。(重點(diǎn))
- params:定義請求的 URL 中必須包含的參數(shù)?;蛘卟话承﹨?shù)。(了解)
- headers:定義請求中 Request Headers 必須包含的參數(shù)?;蛘卟话承﹨?shù)。(了解)
3. RequestMapping 用法
@RequestMapping 有兩種標(biāo)注方式,一種是標(biāo)注在類級別上,一種是標(biāo)注在方法級別上。標(biāo)注在方法上時,value 表示訪問該方法的 URL 地址。標(biāo)注在類上時,value 相當(dāng)于一個命名空間,即訪問該 Controller 下的任意方法都需要帶上這個命名空間。
value屬性
@Controller @RequestMapping("/example") public class ExampleController { @RequestMapping public String execute(){ return "example_page"; } @RequestMapping("/todo") public String doSomething(){ return "example_todo_page"; } }
- /example.action:執(zhí)行的是 execute() 方法。execute() 方法的 @RequestMapping 注解缺省 value 值,在這種情況下,當(dāng)訪問命名空間時默認(rèn)執(zhí)行的是這個方法。方法級別上的 @RequestMapping 標(biāo)注是必須的,否則方法無法被正確訪問。
- /example/todo.action執(zhí)行的是 doSomething() 方法。類級別上的 @RequestMapping 標(biāo)注不是必須的,在不寫的情況下,方法上定義的 URL 都是絕對地址,否則,方法上定義的 URL 都是相對于它所在的 Controller 的。
method屬性
指定請求的method類型,可以接受GET,POST,PUT,DELETE等,默認(rèn)是可以接收GET請求和POST請求
@RequestMapping(value = "/register", method = RequestMethod.GET) public String register1(){ return "example_register_get_page"; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String register2(){ return "example_register_post_page"; }
@RequestMapping({"/test02_01","/test02_02"}) public ModelAndView test02(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test01"); modelAndView.setViewName("hello"); return modelAndView; }
- 可以為多個方法映射相同的 URI,不同的 http method 類型,Spring MVC 根據(jù)請求的 method 類型是可以區(qū)分開這些方法的。當(dāng) /example/register.action 是以 GET 的方式提交的時候,Spring MVC 調(diào)用 register1() 來處理請求;若是以 POST 的方式提交,則調(diào) register2() 來處理提交的請求。
- method 若是缺省沒指定,并不是說它默認(rèn)只處理 GET 方式的請求,而是它可以處理任何方式的 http method 類型的請求。指定 method 是為了細(xì)化映射 ( 縮小處理方法的映射范圍 ),在 method 沒有指定的情況下,它的映射范圍是最大的。
params屬性
params:指定request中必須包含某些參數(shù)值,才讓該方法處理
@RequestMapping(value = "/test04",params = "id") public ModelAndView test04(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test04"); modelAndView.setViewName("hello"); return modelAndView; }
請求路徑?jīng)]有id則報錯
params還可以指定請求中屬性值必須為多少
headers屬性
headers 的作用也是用于細(xì)化映射。只有當(dāng)請求的 Request Headers 中包含與 heanders 值相匹配的參數(shù),處理方法才會被調(diào)用。
@RequestMapping(value = "/specify", headers = "accept=text/*") public String specify(){ return "example_specify_page"; }
請求的 Request Headers 中 Accept 的值必須匹配 text/* ( 如 text/html ),方法才會被調(diào)用。
4. RequestMapping支持Ant風(fēng)格的通配符
通配符 | 說明 | 示例 |
? | 匹配一個任意字符 | /a/?b 可以匹配/a/ab;/a/cb。但不能匹配/a/acb之類 |
* | 匹配任意長度的字符 | /a/ *b可以匹配/a/cb;/a/acb。但不能匹配/a/cb/vb |
** | 匹配多層路徑 | 可以匹配/a/ab;/a/acb;/a/ab/abc/…/… |
到此這篇關(guān)于SpringMVC中的@RequestMapping注解解析的文章就介紹到這了,更多相關(guān)@RequestMapping注解解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC中@RequestMapping注解的實(shí)現(xiàn)
- 詳解SpringMVC中的@RequestMapping注解
- SpringBoot中的@RequestMapping注解的用法示例
- Spring MVC-@RequestMapping注解詳解
- SpringMVC?@RequestMapping注解屬性詳細(xì)介紹
- SpringMVC @RequestMapping注解應(yīng)用方法示例講解
- 詳解SpringBoot中@PostMapping注解的用法
- SpringBoot中@GetMapping注解的使用
- SpringMVC @GetMapping注解路徑?jīng)_突問題解決
- Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)
相關(guān)文章
舉例講解Java的RTTI運(yùn)行時類型識別機(jī)制
這篇文章主要介紹了Java的RTTI運(yùn)行時類型識別機(jī)制,包括泛化的Class引用以及類型檢查instanceof等知識點(diǎn),需要的朋友可以參考下2016-05-05java springmvc實(shí)現(xiàn)驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了java springmvc實(shí)現(xiàn)驗(yàn)證碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11springboot中使用redis并且執(zhí)行調(diào)試lua腳本
今天有個項(xiàng)目需要使用redis,并且有使用腳本的需求,本文主要介紹了springboot中使用redis并且執(zhí)行調(diào)試lua腳本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式
這篇文章主要介紹了SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java反射之靜態(tài)加載和動態(tài)加載的簡單實(shí)例
下面小編就為大家?guī)硪黄狫ava反射之靜態(tài)加載和動態(tài)加載的簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10解決Spring調(diào)用Feign報錯:java.io.IOException:Incomplete output
這篇文章主要介紹了解決Spring調(diào)用Feign報錯:java.io.IOException:Incomplete output stream問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04