javascript
SpringMVC学习--参数绑定
- spring參數(shù)綁定過程
從客戶端請求key/value數(shù)據(jù),經(jīng)過參數(shù)綁定,將key/value數(shù)據(jù)綁定到controller方法的形參上。springmvc中,接收頁面提交的數(shù)據(jù)是通過方法形參來接收。而不是在controller類定義成員變更接收。
?
- 默認(rèn)支持的類型
直接在controller方法形參上定義下邊類型的對象,就可以使用這些對象。在參數(shù)綁定過程中,如果遇到下邊類型直接進(jìn)行綁定。
1、HttpServletRequest
通過request對象獲取請求信息。
2、HttpServletResponse
通過response處理響應(yīng)信息
3、HttpSession
通過session對象得到session中存放的對象
4、Model/ModelMap
model是一個(gè)接口,modelMap是一個(gè)接口實(shí)現(xiàn)。用:將model數(shù)據(jù)填充到request域。
- 簡單類型
通過@RequestParam對簡單類型的參數(shù)進(jìn)行綁定。如果不使用@RequestParam,要求request傳入?yún)?shù)名稱和controller方法的形參名稱一致,方可綁定成功,如果使用@RequestParam,不用限制request傳入?yún)?shù)名稱和controller方法的形參名稱一致。通過required屬性指定參數(shù)是否必須要傳入,如果設(shè)置為true,沒有傳入?yún)?shù),則會(huì)報(bào)錯(cuò)。
- POJO綁定
1、簡單的POJO
將pojo對象中的屬性名于傳遞進(jìn)來的屬性名對應(yīng),如果傳進(jìn)來的參數(shù)名稱和對象中的屬性名稱一致則將參數(shù)值設(shè)置在pojo對象中
?
2、復(fù)雜的POJO
? 與簡單的POJO差不過,只是在頁面上將頁面屬性設(shè)置為pojo類型的屬性,如:
1 <input type="text" name="items.name" />- 自定義參數(shù)綁定實(shí)現(xiàn)日期類型綁定
對于controller形參中pojo對象,如果屬性中有日期類型,需要自定義參數(shù)綁定。將請求日期數(shù)據(jù)串傳成?日期類型,要轉(zhuǎn)換的日期類型和pojo中日期屬性的類型保持一致。
自定義日期類型綁定:
1 public class CustomDateConverter implements Converter<String,Date>{ 2 3 @Override 4 public Date convert(String source) { 5 //將實(shí)際的字符串轉(zhuǎn)為date類型 6 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 7 try { 8 //轉(zhuǎn)換成功直接返回 9 return sdf.parse(source); 10 } catch (ParseException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 //轉(zhuǎn)換失敗返回null 15 return null; 16 } 17 18 }配置方式:springmvc.xml
1 <mvc:annotation-driven conversion-service="conversionService"/> 2 <!-- 自定義參數(shù)綁定 --> 3 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 4 <!-- 轉(zhuǎn)換器 --> 5 <property name="converters"> 6 <list> 7 <!-- 日期類型轉(zhuǎn)換 --> 8 <bean class="com.luchao.converter.CustomDateConverter"/> 9 </list> 10 </property> 11 </bean>- 集合類型綁定
1、數(shù)組綁定
如果要批量刪除刪除,用戶在頁面選擇多個(gè)商品,批量刪除。
將頁面選擇(多選)的商品id,傳到controller方法的形參,方法形參使用數(shù)組接收頁面請求的多個(gè)商品id。
如下代碼:
?
1 // 批量刪除 2 @RequestMapping(value="/deleteItems") 3 public void deleteItems(String[] item_ids) throws Exception { 4 for (String string : item_ids) { 5 System.out.println(string); 6 } 7 }?
2、list綁定
通常在需要批量提交數(shù)據(jù)時(shí),將提交的數(shù)據(jù)綁定到list<pojo>中,比如:成績錄入(錄入多門課成績,批量提交)。
頁面屬性:
?
<c:forEach items="${itemsList }" var="item" varStatus="status"> <tr> <td><input name="itemsList[${status.index }].name" value="${item.name }"/></td><td><input name="itemsList[${status.index }].price" value="${item.price }"/></td><td><input name="itemsList[${status.index }].createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td><td><input name="itemsList[${status.index }].detail" value="${item.detail }"/></td></tr> </c:forEach>?
controller中將包裝list集合的POJO作為參數(shù)。
3、Map綁定
頁面屬性:
<tr> <td>學(xué)生信息:</td> <td> 姓名:<inputtype="text"name="itemInfo['name']"/> 年齡:<inputtype="text"name="itemInfo['price']"/> </td> </tr>controller中將包裝Map集合的POJO作為參數(shù)。
?
?
如果出現(xiàn)亂碼問題,需要添加一個(gè)攔截器,spring已經(jīng)幫我們實(shí)現(xiàn),配置如下:web.xml
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>utf-8</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>CharacterEncodingFilter</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>另外,也可以將tomcat配置文件添加編碼與工程編碼一致,如下:
1 <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>另外一種方法對參數(shù)進(jìn)行重新編碼:
1 String userName = new 2 String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")ISO8859-1是tomcat默認(rèn)編碼,需要將tomcat編碼后的內(nèi)容按utf-8編碼。最后一種實(shí)現(xiàn)比較麻煩。
現(xiàn)在對SpringMVC已經(jīng)有了一定的認(rèn)識(shí),比較其與struts2的一些特點(diǎn)可以加強(qiáng)認(rèn)識(shí)。
1、springmvc基于方法開發(fā)的,struts2基于類開發(fā)的。
springmvc將url和controller方法映射。映射成功后springmvc生成一個(gè)Handler對象,對象中只包括了一個(gè)method。方法執(zhí)行結(jié)束,形參數(shù)據(jù)銷毀。springmvc的controller開發(fā)類似service開發(fā)。
2、springmvc可以進(jìn)行單例開發(fā),并且建議使用單例開發(fā),struts2通過類的成員變量接收參數(shù),無法使用單例,只能使用多例。
3、經(jīng)過實(shí)際測試,struts2速度慢,在于使用struts標(biāo)簽,如果使用struts建議使用jstl。
?
轉(zhuǎn)載于:https://www.cnblogs.com/lcngu/p/5510362.html
總結(jié)
以上是生活随笔為你收集整理的SpringMVC学习--参数绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大三下 期中试卷
- 下一篇: 一个项目中说系统分为表现层、控制层、逻辑