Request URI
部分的注解:@PathVariable
處理Request Header
部分的注解:@RequestHeader
,@CookieValue
處理Request Body
部分的注解:@RequestParam
,@RequestBody
處理Attribute
類型的注解:@sessionAttribute
,@ModelAttribute
@RequestMapping URI template
樣式映射時,即url/{param}
,這時param
可以通過@PathVariable
注解綁定它傳過來的值到方法的參數上 @Controller public class RelativePathUriTemplateController { @RequestMapping("/url/{param}") public void getParams(@PathVariable String param) { //.... } } Request
請求的Header
部分的值綁定到方法的參數上 Host localhost:8080 Accept text/html,application/xhtml+xml,application/xml;q=0.9 Accept-Language fr,en-gb;q=0.7,en;q=0.3 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 @RequestMapping("/url") public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive") long keepAlive) { //... } 把request header
部分的 Accept-Encoding
的值,綁定到參數encoding
上了, Keep-Alive header
的值綁定到參數keepAlive
上。
RequestHeader
中關于cookie
的值綁定到方法的參數上@RequestMapping("/url") public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) { //... } Content-Type
不是application/x-www-form-urlencoded
編碼的內容,例如application/json
,application/xml
等通過使用HandlerAdapter
配置的HttpMessageConverter
來解析data body
,然后綁定到相應的Bean
上因為配置有FormHttpMessageConverter
,所以也可以用來處理application/x-www-form-urlencoded
的內容,處理完的結果放在一個MultiValueMap<String,Stirng>
里 @RequestMapping(value = "/url", method = RequestMethod.POST) public void handle(@RequestBody String body) throws IOException { //... } Controller
的方法返回的對象,通過適當的HttpMessageConverter
轉換為指定格式的數據寫入到Response
對象的body
數據區 @ResponseBody @RequestMapping("/") public RedirectView root() { return new RedirectView("/index/index.html"); }HttpSession
中的Attribute
對象的值 @Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm { // ... } @RequestMapping
之前,為請求綁定需要從后臺查詢的Model
@ModelAttribute public Account addAccount(@RequestParam String number) { return accountManager.findAccount(number); } 這種方式實際的效果就是在調用@RequestMapping
的方法之前,為request
對象的model
里put("account",Account)
Bean
上,要綁定的值來源于 @SessionAttributes
啟用的Attribute
對象@ModelAttribute
用于方法上時指定的Model
對象以上兩種情況都沒有時,new
一個需要綁定的Bean
對象,然后把Request
中按名稱對應的方式把值綁定到Bean
中 @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST) public String PRocessSubmit(@ModelAttribute Pet pet) { } 首先查詢 @SessionAttributes
有無綁定的Pet對象,若沒有則查詢@ModelAttribute
方法層面上是否綁定了Pet對象,若沒有則將URI template
中的值按對應的名稱綁定到Pet對象的各屬性上
轉自http://blog.csdn.net/walkerjong/article/details/7946109
|
新聞熱點
疑難解答