SpringMVC中@RequestMapping(value="/page/{id}",method=RequestMethod.GET)
中的value值可以指定處理請求的url挟鸠、method可以指定處理請求的方式锄弱,SpringMvc可以通過@RequestParam
、@PathVariable
等注解動態(tài)獲取調(diào)用方傳來的參數(shù)庆捺。但SpringMvc獲取部分參數(shù)的注解是不相同的诬像,下面我們就來看一下SpringMvc獲取不同部分參數(shù)的注解脱篙。
1.接收Request uri部分中參數(shù)的注解:@PathVariable;
當(dāng)使用@RequestMapping URI templeate
樣式映射時圃酵,即/{userId}/getById
,這時的userId可通過@PathVariable
注解泞莉,將它傳過來的值綁定到方法參數(shù)上。
@Controller
@RequestMapping("/user/{userId}")
public class RelativePathUriTemplateController {
@RequestMapping("/books/{bookId}")
public void findPet(@PathVariable String userId, @PathVariable String bookId) {
}
}
上面代碼將URI template中變量userId的值和bookId的值钾麸,綁定到方法的參數(shù)上更振。若方法的參數(shù)名稱和需要綁定的template中的變量名稱不一致,需要在@PathVariale("userId")
指定uri template中的名稱饭尝。
2.接收request header部分的注解@RequestHeader,@CookieValue;
@RequestHeader
注解肯腕,可以把Request請求中header部分的值綁定但方法參數(shù)上。
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
Controller中使用@RequestHeader
獲取header中的參數(shù):
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
}
上面的代碼钥平,將request header部分的Accept-Encoding的值实撒,綁定到encoding上了,Keep-Alive header的值綁定到參數(shù)KeepAlive上涉瘾。
@CookieValue可以將Request header中關(guān)于cookie的值綁定到方法的參數(shù)上知态。
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
}
上面就是將JSESSIONID的值綁定到參數(shù)cookie上。
3.接收request body部分的注解:@RequestParam,@RequestBody;
@RequestParam使用場景:
A)常用來處理簡單類型的綁定立叛,通過Reqeust.getParameter()
獲取的String可以直接轉(zhuǎn)化為簡單類型的情況负敏;因?yàn)槭褂?code>request.getParameter()方式獲取參數(shù),所以可以處理get方式中queryString的值秘蛇,也可以獲取Post方式中body data的值其做;
B)用來處理Content-Type:為application/x-www-form-urlencoded
編碼的內(nèi)容,提交方式GET彤叉、POST庶柿;
C)該注解有兩個屬性:value、required;value用來指定要傳入值得id名稱秽浇,required用來指示參數(shù)是否必須綁定浮庐;
@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
}
@RequestBody作用:
該注解常用來處理Content-Type:不是 application/x-www-form-urlencoded
編碼的內(nèi)容,例如application/json,application/xml
等柬焕; 它是通過HandlerAdapter配置的HttpMessageConverters
來解析post data body审残,然后綁定到相應(yīng)的bean上。 因?yàn)榕渲糜?code>FormHttpMessageConverter,所以也可以用來處理Application/x-www-form-urlencoded
的內(nèi)容斑举,處理完的結(jié)果放在一個MultiValueMap<String,String>
里搅轿,這種情況特殊需求下使用,詳情查看FormHttpMessageConverter api
;
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
4.接收attribute類型的注解:@SessionAttributes,@ModelAttribute;
@SessionAttributes:
該注解用來綁定HttpSession中的attribute對象的值富玷,便于在方法中的參數(shù)中使用璧坟。
該注解有value既穆、types兩個屬性,可以通過名字和類型指定要使用的attribute對象雀鹃;
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
// ...
}
@ModelAttribute
該注解有兩個用法:通常用來處理@RequestMapping之前幻工,為請求綁定需要從后臺查詢的model;
用于參數(shù)上時:用來通過名稱對應(yīng)黎茎,將相應(yīng)名稱的值綁定到注解的參數(shù)bean上囊颅;要綁定的值來源于:
A)@SessionAttribute啟用的attribute對象上;
B)@ModelAttribute用于方法上時指定的model對象傅瞻;
C)上述兩種情況都沒有時踢代,new一個需要綁定的bean對象,然后將request中按名稱對應(yīng)的方式把值綁定到bean中
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
這種方式實(shí)際的效果就是在調(diào)用@RequestMapping的方法之前嗅骄,為request對象的model里put("account",Account);
用在參數(shù)上的@ModelAttribute示例代碼:
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
}
首先查詢@SessionAttributes有無綁定的Pet對象胳挎,若沒有則查詢@ModelAttribute方法層面上是否綁定了Pet對象,若沒有則URI template中的值按對應(yīng)的名稱綁定到Pet對象的各屬性上溺森。
參考文獻(xiàn):http://blog.csdn.net/walkerjong/article/details/7946109