Spring參數(shù)綁定

1.默認(rèn)支持的參數(shù)類型

處理器形參中添加如下類型的參數(shù)處理適配器會默認(rèn)識別并進(jìn)行賦值。

HttpServletRequest:通過request對象獲取請求信息

HttpServletResponse:通過response處理響應(yīng)信息

HttpSession:通過session對象得到session中存放的對象

2.Model和ModelMap

Model? 和? Model

除了ModelAndView以外,還可以使用Model來向頁面?zhèn)鬟f數(shù)據(jù),

Model是一個接口,在參數(shù)里直接聲明model即可。如果使用Model則可以不使用ModelAndView對象,Model對象可以向頁面?zhèn)鬟f數(shù)據(jù),View對象則可以使用String返回值替代。

不管是Model還是ModelAndView,其本質(zhì)都是使用Request對象向jsp傳遞數(shù)據(jù)。

代碼實(shí)現(xiàn):

/**

?* 根據(jù)id查詢商品,使用Model

?* @param?request

?* @param?model

?* @return

?*/

@RequestMapping("/itemEdit")

public?String queryItemById(HttpServletRequest request,?Model model) {

// 從request中獲取請求參數(shù)

String strId?= request.getParameter("id");

Integer id?= Integer.valueOf(strId);

// 根據(jù)id查詢商品數(shù)據(jù)

Item item?= this.itemService.queryItemById(id);

// 把結(jié)果傳遞給頁面

// ModelAndView modelAndView = new ModelAndView();

// 把商品數(shù)據(jù)放在模型中

// modelAndView.addObject("item", item);

// 設(shè)置邏輯視圖

// modelAndView.setViewName("itemEdit");

// 把商品數(shù)據(jù)放在模型中

model.addAttribute("item", item);

return?"itemEdit";

}


/**

?* 根據(jù)id查詢商品 使用ModelView

?*

?* @param?request

?* @return

?*/

@RequestMapping("/itemEdit")

public?ModelAndView queryItemById(HttpServletRequest request) {

// 從request中獲取請求參數(shù)

String strId?= request.getParameter("id");

Integer id?= Integer.valueOf(strId);

// 根據(jù)id查詢商品數(shù)據(jù)

Item item?= this.itemService.queryItemById(id);

// 把結(jié)果傳遞給頁面

ModelAndView modelAndView?= new?ModelAndView();

// 把商品數(shù)據(jù)放在模型中

modelAndView.addObject("item", item);

// 設(shè)置邏輯視圖

modelAndView.setViewName("itemEdit");

return?modelAndView;

}

ModelMap

ModelMap是Model接口的實(shí)現(xiàn)類,也可以通過ModelMap向頁面?zhèn)鬟f數(shù)據(jù)

使用Model和ModelMap的效果一樣,如果直接使用Model,springmvc會實(shí)例化ModelMap。

代碼實(shí)現(xiàn):

/**

?* 根據(jù)id查詢商品,使用ModelMap

?* @param?request

?* @param?model

?* @return

?*/

@RequestMapping("/itemEdit")

public?String queryItemById(HttpServletRequest request, ModelMap model) {

// 從request中獲取請求參數(shù)

String strId?= request.getParameter("id");

Integer id?= Integer.valueOf(strId);

// 根據(jù)id查詢商品數(shù)據(jù)

Item item?= this.itemService.queryItemById(id);

// 把結(jié)果傳遞給頁面

// ModelAndView modelAndView = new ModelAndView();

// 把商品數(shù)據(jù)放在模型中

// modelAndView.addObject("item", item);

// 設(shè)置邏輯視圖

// modelAndView.setViewName("itemEdit");

// 把商品數(shù)據(jù)放在模型中

model.addAttribute("item", item);

return?"itemEdit";

}

綁定簡單類型

當(dāng)請求的參數(shù)名稱和處理器形參名稱一致時會將請求參數(shù)與形參進(jìn)行綁定。

這樣,從Request取參數(shù)的方法就可以進(jìn)一步簡化。

/**

?* 根據(jù)id查詢商品,綁定簡單數(shù)據(jù)類型

?*

?* @param?id

?* @param?model

?* @return

?*/

@RequestMapping("/itemEdit")

public?String queryItemById(int?id, ModelMap model) {

// 根據(jù)id查詢商品數(shù)據(jù)

Item item?= this.itemService.queryItemById(id);

// 把商品數(shù)據(jù)放在模型中

model.addAttribute("item", item);

return?"itemEdit";

}

3.支持的數(shù)據(jù)類型

參數(shù)類型推薦使用包裝數(shù)據(jù)類型,因?yàn)榛A(chǔ)數(shù)據(jù)類型不可以為null

整形:Integer、int

字符串:String

單精度:Float、float

雙精度:Double、double

布爾型:Boolean、boolean

說明:對于布爾類型的參數(shù),請求的參數(shù)值為true或false?;蛘?或0

請求url:

http://localhost:8080/xxx.action?id=2&status=false

處理器方法:

public String editItem(Model model,Integer id,Boolean status)


注解

@RequestParam

使用@RequestParam常用于處理簡單類型的綁定。

value:參數(shù)名字,即入?yún)⒌恼埱髤?shù)名字,如value=“itemId”表示請求的參數(shù) 區(qū)中的名字為itemId的參數(shù)的值將傳入

required:是否必須,默認(rèn)是true,表示請求中一定要有相應(yīng)的參數(shù),否則將報錯

TTP Status 400 - Required Integer parameter 'XXXX' is not present

defaultValue:默認(rèn)值,表示如果請求中沒有同名參數(shù)時的默認(rèn)值

定義如下:

@RequestMapping("/itemEdit")

public?String queryItemById(@RequestParam(value = "itemId", required = true, defaultValue = "1") Integer id,

ModelMap modelMap) {

// 根據(jù)id查詢商品數(shù)據(jù)

Item item?= this.itemService.queryItemById(id);

// 把商品數(shù)據(jù)放在模型中

modelMap.addAttribute("item", item);

return?"itemEdit";

}

解決post亂碼問題

在web.xml中加入:

<!-- 解決post亂碼問題 -->

<filter>

<filter-name>encoding</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<!-- 設(shè)置編碼參是UTF8 -->

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

以上可以解決post請求亂碼問題。

對于get請求中文參數(shù)出現(xiàn)亂碼解決方法有兩個:

修改tomcat配置文件添加編碼與工程編碼一致,如下:

<Connector URIEncoding="utf-8"?connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

另外一種方法對參數(shù)進(jìn)行重新編碼:

String userName new

String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat默認(rèn)編碼,需要將tomcat編碼后的內(nèi)容按utf-8編碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容