SpringMVC-@RequestMapping的參數和用法


RequestMapping里面的注解包含的參數如圖:

這里寫圖片描述

RequestMapping是一個用來處理請求地址映射的注解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。


@RequestMapping 除了修飾方法, 還可來修飾類 :

類定義處: 提供初步的請求映射信息。相對於 WEB 應用的根目錄;

方法處: 提供進一步的細分映射信息。 相對於類定義處的 URL。

若類定義處未標注 @RequestMapping,則方法處標記的 URL相對於 WEB 應用的根目錄

返回ModelAndView時的url會根據你的 @RequestMapping實際情況組成。

如果類上沒有映射,那么url直接就是方法的映射;否則url為類上+方法上映射路徑組合。

對應項目jsp位置則是一級路徑對應一級文件目錄。

如url為/default/index對應項目中webapp/default/index.jsp


RequestMapping注解有六個屬性,下面我們把她分成三類進行說明。

【1】value, method

value:指定請求的實際地址,指定的地址可以是URI Template 模式;

method: 指定請求的method類型, GET、POST、PUT、DELETE等;


【2】consumes,produces

consumes: 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;

produces: 指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;


【3】params,headers

params: 指定request中必須包含某些參數值時,才讓該方法處理。

headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。


測試示例如下:

【1】value||path

  • JSP 頁面
<a href="springmvc/testRequestMapping">Test RequestMapping</a>
  • 1
  • controller
    @RequestMapping(value="/testRequestMapping" ) public String testRequestMapping() { System.out.println("testRequestMapping"); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5

成功返回success.jsp 。

Tips :若 href 屬性值,不等於value值,則將提示404錯誤。


value的uri值為以下三類:

A) 可以指定為普通的具體值;

如下:

@RequestMapping("/testRequestMapping")
  • 1

B) 可以指定為含有某變量的一類值(URI Template Patterns with Path Variables)–restful風格;

    @RequestMapping("/testPathVariable/{id}") public String testPathVariable(@PathVariable Integer id2) { System.out.println("testPathVariable: " + id2); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5

除了value還有path,二者效果等同,可以參考源碼如下圖:

這里寫圖片描述


其中關於@PathVariable 有如下說明:

① 如果路徑中的變量與方法中的變量名一致,可直接使用@PathVariable;

② 如果二者不一致,則使用@PathVariable(Variable)顯示指定要綁定的路徑中的變量 。

@PathVariable只能綁定路徑中的占位符參數,且路徑中必須有參數。

@PathVariable用法參考路徑參數綁定參考

    @RequestMapping("/testPathVariable/{id}") public String testPathVariable(@PathVariable("id") Integer id2) { System.out.println("testPathVariable: " + id2); return SUCCESS; } //路徑中的 id 與 方法中的 id2 綁定
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

C) 可以指定為含正則表達式的一類值( URI Template Patterns with Regular Expressions);

  @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}") public void handle(@PathVariable String version, @PathVariable String extension) { // ... } } 
  • 1
  • 2
  • 3
  • 4
  • 5

【2】method

  • JSP 頁面
<a href="springmvc/testMethod">Test Method</a> //href 默認為get 請求。
  • 1
  • 2
  • controller–限制接收post 請求。
    @RequestMapping(value = "/testMethod", method = RequestMethod.POST) public String testMethod() { System.out.println("testMethod"); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • result as follows :

HTTP Status 405 - Request method ‘GET’ not supported 。

這里寫圖片描述

【狀態碼405表示:請求中指定的方法不被允許。】

將method 改為method = RequestMethod.GET正常跳轉頁面。


【3】consumes

  • JSP 頁面

仍以testMethod為例,提交表單。

默認contentType為Content-Type:application/x-www-form-urlencoded。

    <form action="springmvc/testMethod" method="POST"> <input type="text" name="username" value=""/> <input type="submit" value="submit"/> </form> 
  • 1
  • 2
  • 3
  • 4
  • 5

這里寫圖片描述


  • controller–限制接收post 請求以及consumes="application/json"
    @RequestMapping(value = "/testMethod", method = RequestMethod.POST,consumes="application/json") public String testMethod() { System.out.println("testMethod"); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • result as follows :

這里寫圖片描述

【狀態碼415表示:由於媒介類型不被支持,服務器不會接受請求。。】

去掉 consumes屬性,頁面正常跳轉 !


【4】produces

后台代碼如下:

    @RequestMapping(value = "/testMethod", method = RequestMethod.POST,produces="application/json") public void testMethod2(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException { request.getHeader("Accept"); System.out.println(request.getHeader("Accept")); // response.setContentType("application/json"); String username = request.getParameter("username"); System.out.println("testMethod..."+username); model.addAttribute("user", username); Object jsonString = "{'name': 'helloworlda'}"; JSONObject jsonobj=JSONObject.fromObject(jsonString); PrintWriter out = response.getWriter(); out.print(jsonobj); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 瀏覽器請求頭
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  • 1

其中最后一項 : */*;q=0.8

該項表明可以接收任何類型的,權重系數0.8表明如果前面幾種類型不能正常接收。則使用該項進行自動分析。

application/json 幾種主流瀏覽器都可以自動解析。


【5】params

  • JSP頁面
    form action="springmvc/testParamsAndHeaders" method="POST"> <input type="text" name="username" value=""/> <input type="text" name="age" value=""/> <input type="submit" value="submit"/> </form>
  • 1
  • 2
  • 3
  • 4
  • 5

參數 username=tom;age = 10;

  • 后台代碼:

設定必須包含username 和age兩個參數,且age參數不為10 (可以有多個參數)。

@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }) public String testParamsAndHeaders() { System.out.println("testParamsAndHeaders"); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • result as follows :

這里寫圖片描述

【狀態碼400表示:服務器未能理解請求。 】

  • 將age 改為其他值,正常跳轉。

這里寫圖片描述


【6】headers

  • 瀏覽器請求頭如下:

這里寫圖片描述

  • 后台測試代碼如下:
@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "Accept-Language=US,zh;q=0.8" }) public String testParamsAndHeaders() { System.out.println("testParamsAndHeaders"); return SUCCESS; }
  • 1
  • 2
  • 3
  • 4
  • 5

設定請求頭中第一語言必須為US。

  • result as follows :

這里寫圖片描述

【狀態碼404表示:服務器無法找到被請求的頁面。】

將后台代碼改為zh-CN。。。

這里寫圖片描述

頁面正常跳轉。


【Tips】:

① 服務器首先根據URL去找頁面,如果找不到就返回404;

② 如果找到,但是不能正常處理,就會返回 5XX 類型錯誤。

其中在第一步過程中,會根據請求頭進行一系列判斷 !


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM