RequestMapping既能夠修飾方法,也能夠修飾類html
1. @RequestMapping不只能夠根據url來響應用戶請求,還能夠根據其餘參數來響應:java
他們之間是」與「的關係,聯合使用多個條件可讓請求映射更加精確化web
eg. (經常使用)使用 method 屬性來指定請求方式(params 和 headers 比較不經常使用)spring
/** * 經常使用: 使用 method 屬性來指定請求方式 */ @RequestMapping(value = "/testMethod", method = RequestMethod.POST) public String testMethod() { System.out.println("testMethod"); return SUCCESS; }
2. params和headers支持簡單的表達式:數據庫
使用params和headers來精確映射請求(瞭解便可):瀏覽器
如上圖,要求請求中包含參數「username」和「age」,而且age不等於10;同時要求請求頭中的信息中包含「Accept-Language-US,zh;q=0.8」服務器
因此,當請求爲以下時,請求沒法響應而會報錯。網絡
?:匹配文件名中的一個字符架構
*:匹配文件名中的任意字符 mvc
**:匹配多層路徑
- /user/*/createUser:匹配 /user/aaa/createUser、/user/bbb/createUser 等 URL
- /user/**/createUser:匹配 /user/createUser、/user/aaa/bbb/createUser 等 URL
- /user/createUser??: 匹配 /user/createUseraa、/user/createUserbb 等 URL
帶佔位符的 URL 是spring3.0 新增的功能,該功能在springmvc向 REST 目標挺進發展過程當中具備里程碑式的意義
經過 @PathVariable 註解能夠將 URL 中的佔位符參數綁定到到目標方法的入參中,即:
URL 中的 {xxx} 佔位符能夠經過@PathVariable(「xxx」)綁定到操做方法的入參中
/** * @PathVariable 能夠來映射 URL 中的佔位符到目標方法的參數中. * @param id * @return */ @RequestMapping("/testPathVariable/{id}") public String testPathVariable(@PathVariable("id") Integer id) { System.out.println("testPathVariable: " + id); return SUCCESS; }
講 HiddenHttpMethodFilter 以前,有必要先介紹一下REST:
REST:即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易於理解、擴展方便,因此正獲得愈來愈多網站的採用。
- 資源(Resources):網絡上的一個實體,或者說是網絡上的一個具體信息。它能夠是一段文本、一張圖片、一首歌曲、一種服務,總之就是一個具體的存在。能夠用一個URI(統一資源定位符)指向它,每種資源對應一個特定的 URI 。要獲取這個資源,訪問它的URI就能夠,所以 URI 即爲每個資源的獨一無二的識別符。
- 表現層(Representation):把資源具體呈現出來的形式,叫作它的表現(Representation)。好比,文本能夠用 txt 格式表現,也能夠用 HTML 格式、XML 格式、JSON 格式表現,甚至能夠採用二進制格式。
- 狀態轉化(State Transfer):每發出一個請求,就表明了客戶端和服務器的一次交互過程。HTTP協議,是一個無狀態協議,即全部的狀態都保存在服務器端。所以,若是客戶端想要操做服務器,必須經過某種手段,讓服務器端發生「狀態轉化」(State Transfer)。而這種轉化是創建在表現層之上的,因此就是 「表現層狀態轉化」。具體說,就是 HTTP 協議裏面,四個表示操做方式的動詞:GET、POST、PUT、DELETE。它們分別對應四種基本操做:GET 用來獲取資源,POST 用來新建資源,PUT 用來更新資源,DELETE 用來刪除資源。
四種狀態分別對應了數據庫的增刪改查操做,例如:
– /order/1 HTTP GET :獲得 id = 1 的 order
– /order/1 HTTP DELETE:刪除 id = 1的 order
– /order/1 HTTP PUT:更新id = 1的 order
– /order HTTP POST:新增 orderHiddenHttpMethodFilter:瀏覽器 form 表單只支持 GET與 POST 請求,而DELETE、PUT 等 method 並不支持,Spring3.0 添加了一個過濾器,能夠將這些請求轉換爲標準的 http 方法,使得支持 GET、POST、PUT 與DELETE 請求。
具體應用步驟:
1. 首先在web.xml中添加HiddenHttpMethodFilter的配置:
<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 能夠把 POST 請求轉爲 DELETE 或 POST 請求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2. 請求連接:
<a href="springmvc/testREST/1">test REST GET</a> <br><br> <form action="springmvc/testREST" method="post"> <input type="submit" value="test REST POST"> </form> <br><br> <form action="springmvc/testREST/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="test REST DELETE"> </form> <br><br> <form action="springmvc/testREST/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="test REST PUT"> </form>
3. handler處理:
package com.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/springmvc") public class SpringMVCTest { private static final String SUCCESS="success"; /** * Rest 風格的 URL. * 以 CRUD 爲例: * 新增: /order POST * 修改: /order/1 PUT update?id=1 * 獲取: /order/1 GET get?id=1 * 刪除: /order/1 DELETE delete?id=1 * * 如何發送 PUT 請求和 DELETE 請求呢 ? * 1. 須要配置 HiddenHttpMethodFilter * 2. 須要發送 POST 請求 * 3. 須要在發送 POST 請求時攜帶一個 name="_method" 的隱藏域, 值爲 DELETE 或 PUT * * 在 SpringMVC 的目標方法中如何獲得 id 呢? 使用 @PathVariable 註解 * */ @RequestMapping(value="/testREST/{id}" , method=RequestMethod.GET) public String testREST(@PathVariable("id") Integer id) { System.out.println("testREST GET : "+id); return SUCCESS; } @RequestMapping(value="/testREST" , method=RequestMethod.POST) public String testREST() { System.out.println("testREST POST"); return SUCCESS; } @RequestMapping(value="/testREST/{id}" , method=RequestMethod.DELETE) public String testRESTdelete(@PathVariable("id") Integer id) { System.out.println("testREST DELETE:"+id); return SUCCESS; } @RequestMapping(value="/testREST/{id}" , method=RequestMethod.PUT) public String testRESTput(@PathVariable("id") Integer id) { System.out.println("testREST PUT:"+id); return SUCCESS; } }