SpringMvc之引數繫結註解詳解之二

2022-06-29 21:33:12 字數 1198 閱讀 7685

cousumes的樣例:

1 @controller  

3 public voidaddpet(@requestbody pet pet, model model)   

produces的樣例:

1 @controller  

3 @responsebody  

4 publicpet getpet(@pathvariable string petid, model model)   

params的樣例:

1 @controller  

3 public classrelativepathuritemplatecontroller   

9 }  

僅處理請求中包含了名為「myparam」,值為「myvalue」的請求;

headers的樣例:

1 @controller  

3 public classrelativepathuritemplatecontroller   

9 }  

僅處理request的header中包含了指定「refer」請求頭和對應值為「」的請求;

handler method 引數繫結常用的註解,我們根據他們處理的request的不同內容部分分為四類:(主要講解常用型別)

a、處理requet uri 部分(這裡指uri template中variable,不含querystring部分)的註解:   @pathvariable;

b、處理request header部分的註解:   @requestheader, @cookievalue;

c、處理request body部分的註解:@requestparam,  @requestbody;

d、處理attribute型別是註解: @sessionattributes, @modelattribute;

示例**:

@controller  

public class relativepathuritemplatecontroller   

}  上面**把uri template 中變數 ownerid的值和petid的值,繫結到方法的引數上。若方法引數名稱和需要繫結的uri template中變數名稱不一致,需要在@pathvariable("name")指定uri template中的名稱。

SpringMVC之引數繫結

httpservletrequest httpservletresponse httpsession model modelmap 直接在形參上給出這些預設型別的宣告,就能直接使用了 基本資料型別,如果從前台頁面傳遞的值為 null 或者 的話,那麼會出現資料轉換的異常,最好將引數資料型別定義成包裝...

spring MVC 引數繫結

spring mvc的引數可以繫結簡單型別 pojo型別和自定義型別。1 controller預設支援的引數型別 這些物件只要在controller形參中定義就可以使用。2 簡單引數型別 此種方法需要遵循的的原則是定義的形參要與請求中的引數名一致 如果想要讓形參的名與請求中攜帶的引數名不一致,就需要...

spring mvc引數繫結

controller public class hellocontroller 當請求的引數名稱和處理器形參名稱一致時會將請求引數與形參進行繫結。spring會自動將表單引數注入到方法引數,不需要做任何處理。從request獲取引數的方法可以進一步簡化 public modelandview dem...