国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

Spring數據傳遞中注解的使用

2024-04-27 15:12:36
字體:
來源:轉載
供稿:網友

根據處理Request的不同內容分為4類:

處理Request URI部分的注解:@PathVariable處理Request Header部分的注解:@RequestHeader@CookieValue處理Request Body部分的注解:@RequestParam@RequestBody處理Attribute類型的注解:@sessionAttribute@ModelAttribute

@PathVariable

當使用@RequestMapping URI template樣式映射時,即url/{param},這時param可以通過@PathVariable注解綁定它傳過來的值到方法的參數上 @Controller public class RelativePathUriTemplateController { @RequestMapping("/url/{param}") public void getParams(@PathVariable String param) { //.... } }

@RequestHeader

可以把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上。


@CookieValue

可以把RequestHeader中關于cookie的值綁定到方法的參數上@RequestMapping("/url") public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) { //... }

@RequestBody

通常用來處理Content-Type不是application/x-www-form-urlencoded編碼的內容,例如application/jsonapplication/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 { //... }

@ResponseBody

該注解用于將Controller的方法返回的對象,通過適當的HttpMessageConverter轉換為指定格式的數據寫入到Response對象的body數據區 @ResponseBody @RequestMapping("/") public RedirectView root() { return new RedirectView("/index/index.html"); }

@SessionAttributes

用來綁定HttpSession中的Attribute對象的值 @Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm { // ... }

@ModelAttribute

用于方法上時通常用來處理@RequestMapping之前,為請求綁定需要從后臺查詢的Model @ModelAttribute public Account addAccount(@RequestParam String number) { return accountManager.findAccount(number); }

這種方式實際的效果就是在調用@RequestMapping的方法之前,為request對象的modelput("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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富顺县| 桃园市| 宜昌市| 旬阳县| 津市市| 若尔盖县| 眉山市| 新兴县| 吴川市| 灵川县| 株洲县| 平原县| 安平县| 临夏县| 瑞昌市| 鄂州市| 双城市| 温宿县| 麻阳| 禄丰县| 筠连县| 揭阳市| 长丰县| 都昌县| 长岭县| 普安县| 乐都县| 福鼎市| 韶关市| 微山县| 高尔夫| 开阳县| 黔江区| 霞浦县| 周口市| 吐鲁番市| 肥城市| 砚山县| 综艺| 庄河市| 双牌县|