久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      springmvc常用5種注解的使用

      springmvc的常見5種注解:1、@RequestMapping,是一個用來處理請求地址映射的注解;2、@RequestParam,用于將請求參數(shù)區(qū)數(shù)據(jù)映射到功能處理方法的參數(shù)上;3、@PathVariable,用于將請求變量。

      springmvc常用5種注解的使用

      @RequestMapping

      是一個用來處理請求地址映射的注解
      適用于類、方法。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。

      屬性

      value: 指定請求的實(shí)際地址,值可以是普通的具體值,可以指定為含有某變量的一類值(URI Template Patterns with Path Variables)
      可以指定為含正則表達(dá)式的一類值(URI Template Patterns with Regular Expressions)
      method: 指定請求的method類型,GET、POST、PUT、DELETE等
      consumes: 指定處理請求的提交內(nèi)容類型(Content-Type),例如application/json, text/html
      produces: 指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回
      params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理
      headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求

        1.處理get請求: @RequestMapping(value = "index",method = RequestMethod.GET)   2.springboot錯誤處理(使用app客戶端返回json格式,使用瀏覽器返回html錯誤頁)    @RequestMapping(produces = "text/html")   3.方法僅處理request Content-Type為“application/json”類型的請求    @RequestMapping(value = "/pets", consumes="application/json")   4.僅處理請求中包含了名為“myParam”,值為“myValue”的請求    @RequestMapping(value = "/pets/{petId}", params="myParam=myValue")    5.僅處理request的header中包含了指定“Refer”請求頭和對應(yīng)值為“http://www.rxy.com/”的請求    @RequestMapping(value = "/pets", headers="Referer=http://www.rxy.com/")

      @RequestParam

      用于將請求參數(shù)區(qū)數(shù)據(jù)映射到功能處理方法的參數(shù)上
      適用:方法參數(shù)

      屬性

      value/name: 兩個屬性都指代參數(shù)名字,即入?yún)⒌恼埱髤?shù)名字(通常表單name屬性)
      required: 是否必須,默認(rèn)是true,表示請求中一定要有相應(yīng)的參數(shù),否則將拋出異常
      defaultValue: 默認(rèn)值,表示如果請求中沒有同名參數(shù)時的默認(rèn)值,設(shè)置該參數(shù)時,自動將required設(shè)為false

        如果是原子類型,不管加沒加注解,都必須有值,否則拋出異常,如果允許空值請使用包裝類代替   index(@RequestParam Integer num){}  表示該參數(shù)必須傳遞,值允許為空   表示該參數(shù)非必須,如果不傳則默認(rèn)為0   getPageData(@RequestParam(name="pageNum",defaultValue="0") String pageNo, String pageSize)

      @PathVariable

      用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上,即取出uri模板中的變量作為參數(shù)
      適用:方法參數(shù)

      屬性

      value: 指定url模版變量名稱,如果名稱與方法參數(shù)名不一樣,則需要指定,否則可省略

          @RequestMapping("/index/{id}")          public String index(@PathVariable("id") String sdf){            System.out.println(sdf);            return "index";      }

      @ResponseBody

      該注解用于將Controller的方法返回的對象,通過適當(dāng)?shù)?code>HttpMessageConverter轉(zhuǎn)換為指定格式后,寫入到Response對象的body數(shù)據(jù)區(qū),默認(rèn)springmvcjson形式返回(使用jackson轉(zhuǎn)換器)
      適用:方法,返回的數(shù)據(jù)不是html標(biāo)簽的頁面,而是其他某種格式的數(shù)據(jù)時(如json、xml等)使用
      對比:@RequestBody 將HTTP請求正文轉(zhuǎn)換為適合的HttpMessageConverter對象
      @ResponseBody 將內(nèi)容或?qū)ο笞鳛?HTTP 響應(yīng)正文返回,并調(diào)用適合HttpMessageConverter的Adapter轉(zhuǎn)換對象,寫入輸出流

      @RequestBody

      1.該注解用于讀取Request請求的body部分?jǐn)?shù)據(jù),使用系統(tǒng)默認(rèn)配置的HttpMessageConverter進(jìn)行解析,然后把相應(yīng)的數(shù)據(jù)綁定到要返回的對象上
      2.再把HttpMessageConverter返回的對象數(shù)據(jù)綁定到controller中方法的參數(shù)上
      適用:方法參數(shù),對于request的Content-Type:為application/json, application/xml必須使用該注解
      對于application/x-www-form-urlencoded,如果請求方式為put,則為必須,
      對于POST/GET方式可選(即非必須,因?yàn)锧RequestParam,@ModelAttribute也可以處理)
      對于multipart/form-data, @RequestBody不能處理這種格式的數(shù)據(jù)
      屬性:required: 是否必須,默認(rèn)是true,表示請求中一定要有相應(yīng)的參數(shù),否則將拋出異常
      示例:通常使用該注解前端都是發(fā)送ajax請求,那么請求部分內(nèi)容如下:

               $.ajax({            type: "POST",            url:"/role/saveRole",            contentType:"application/json",            data: JSON.stringify(_self.form)...

      注意:contentType不可省略,data必須是通過stringify轉(zhuǎn)換為json字符串的數(shù)據(jù)
      那么對應(yīng)方法就可以這么寫:

      @RequestMapping(value = "/saveRole",method = RequestMethod.POST) public String saveRole(@RequestBody People requestJson) {}

      如果前端傳遞的是對象的數(shù)組,那么后臺也可以直接使用List<Obj>直接接受,這是綁定List數(shù)據(jù)最實(shí)用的方式

      @CookieValue

      可以把Request header中關(guān)于cookie的值綁定到方法的參數(shù)上
      適用:方法參數(shù)

      獲取cookie中的JSESSIONID public String index(@CookieValue("JSESSIONID") String cookie){}

      @RequestHeader

      可以把Request請求header部分的值綁定到方法的參數(shù)上
      適用:方法參數(shù)

      獲取請求中Accept-Encoding值,返回gzip, deflate, br public String index(@RequestHeader("Accept-Encoding") String host){return host;}

      @ExceptionHandler

      注解在方法上,表示該方法用于處理特定的異常,處理范圍是當(dāng)前類,如果想要全局捕獲異常,需要使用@ControllerAdvice
      當(dāng)一個Controller中有多個HandleException注解出現(xiàn)時,那么異常被哪個方法捕捉呢?這就存在一個優(yōu)先級的問題
      ExceptionHandler的優(yōu)先級是:在異常的體系結(jié)構(gòu)中,哪個異常與目標(biāo)方法拋出的異常血緣關(guān)系越緊密,就會被哪個捕捉到
      屬性:value: 需要處理的異常類型集合(Class)
      在當(dāng)前Controller有兩個處理異常的方法,當(dāng)訪問/index時,頁面顯示: json data

      package com.rxy.controller;  @Controller public class HelloController {          @ExceptionHandler({ ArithmeticException.class })     @ResponseBody     public String handleArithmeticException(Exception e) {         e.printStackTrace();         return "json data";     }          @ExceptionHandler({ IOException.class })     public String handleIOException(Exception e) {         e.printStackTrace();         //返回錯誤頁面         return "error";     }      @RequestMapping("/index")     public String index(){         int i = 10 / 0;         return "index";     }  }

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號