本文主要給大家介紹了關(guān)于Spring MVC數(shù)據(jù)轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
成都創(chuàng)新互聯(lián)公司是一家以成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、品牌設(shè)計、軟件運(yùn)維、成都網(wǎng)站推廣、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為服務(wù)器托管等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
數(shù)據(jù)綁定
SpringMVC負(fù)責(zé)將request中的信息以一定的方式轉(zhuǎn)換并綁定到處理方法的參數(shù)上。整個過程的處理核心是由DataBinder完成。轉(zhuǎn)換流程如下:
1.DataBinder從ServletRequest中獲取參數(shù)信息;
2.DataBinder獲取處理方法的參數(shù);
3.DataBinder調(diào)用ConversionService組件數(shù)據(jù)類型轉(zhuǎn)換和數(shù)據(jù)格式化工作,并將轉(zhuǎn)化結(jié)果填充到參數(shù)對象中;
4.DataBinder調(diào)用Validator組件進(jìn)行數(shù)據(jù)的校驗(yàn)工作;
5.經(jīng)歷以上步驟后,DataBinder將生成BinderResult對象,BinderResult中包含轉(zhuǎn)換后的信息,也包含校驗(yàn)后的錯誤信息。
數(shù)據(jù)轉(zhuǎn)換
在java語言中,在java.beans包中提供了一個PropertyEditor接口來進(jìn)行數(shù)據(jù)轉(zhuǎn)換,PropertyEditor的核心功能是將一個String轉(zhuǎn)換為一個java對象。Spring從3.0開始添加一個通用的類型轉(zhuǎn)換模塊即為org.springframework.convert包中,ConversionService是org.springframework.convert包的核心組件,可以通過使用ConversionServiceFactoryBean在spring的上下文中自定義一個ConversionService,Spring將自動識別這個ConversionService,并在SpringMVC進(jìn)行參數(shù)轉(zhuǎn)換時使用,配置例子如下所示:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="org.xx..StringToDateConverter" /> </list> </property> </bean>
SpringMVC在支持新的轉(zhuǎn)換器框架的同時,也支持javabeans的PropertyEditor,可以在控制器中使用@InitBinder添加自定義的編輯器。
舉例如下:
@Controller public class DataBinderTestController { @RequestMapping(value = "/dataBind") public String test(DataBinderTestModel command) { ...... } @InitBinder public void iniiBinder(WebDataBinder binder){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); format.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(format, false)); } }
各種轉(zhuǎn)換器的優(yōu)先順序:
1.查詢通過@InitBinder自定義的編輯器;
2.查詢通過ConversionService裝配的自定義轉(zhuǎn)換器;
3.查詢通過WebBindingInitializer接口裝配的全局自定義編輯器。
Formater
除了org.springframework.core.convert.converter接口中定義的三種類型的轉(zhuǎn)換器接口,SpringMVC在org.springframework.format包中還提供了一些格式化轉(zhuǎn)換接口,format和converter的最大的區(qū)別是,converter實(shí)現(xiàn)的是object到object的轉(zhuǎn)換,而format實(shí)現(xiàn)的是從String到Object的轉(zhuǎn)換,format包中最重要的接口是Formater,F(xiàn)ormater的使用示例如下所示:
public class DateFormatter extends Formatter<Date>{ private String datePattern; private SimpleDateFormat dateFormat; public DateFormatter(String datePattern){ this.datePattern=datePattern; this.dateFormat=new SimpleDateFormat(datePattern); } public String pring(Date,Locale locale){ return dateFormat.format(date); } public Date parse(String source,Locale locale) throws ParseException{ try{ return dateFormat.parse(source); }catch(Exception e){ ...... } } }
最后再將DateFormatter注入到ConversionService中,注入方式和Converter的注入方式一樣,也可由此發(fā)現(xiàn),ConversionService是數(shù)據(jù)轉(zhuǎn)換的核心。
Format的注解
在org.springframework.format.annotation包中定義了兩個注解,@DateTimeFormat和@NumberFormat 這兩個注解可以用在domain中的屬性上,SpringMVC處理方法參數(shù)綁定數(shù)據(jù)、模型數(shù)據(jù)輸出時會自動通過注解應(yīng)用格式化的功能。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
網(wǎng)站名稱:深入理解SpringMVC的數(shù)據(jù)轉(zhuǎn)換
當(dāng)前路徑:http://chinadenli.net/article18/pgoodp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、動態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站、App開發(fā)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)