本文主要給大家介紹了關(guān)于Spring MVC數(shù)據(jù)轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
數(shù)據(jù)綁定
SpringMVC負(fù)責(zé)將request中的信息以一定的方式轉(zhuǎn)換并綁定到處理方法的參數(shù)上。整個(gè)過(guò)程的處理核心是由DataBinder完成。轉(zhuǎn)換流程如下:
1.DataBinder從ServletRequest中獲取參數(shù)信息;
2.DataBinder獲取處理方法的參數(shù);
3.DataBinder調(diào)用ConversionService組件數(shù)據(jù)類(lèi)型轉(zhuǎn)換和數(shù)據(jù)格式化工作,并將轉(zhuǎn)化結(jié)果填充到參數(shù)對(duì)象中;
4.DataBinder調(diào)用Validator組件進(jìn)行數(shù)據(jù)的校驗(yàn)工作;
5.經(jīng)歷以上步驟后,DataBinder將生成BinderResult對(duì)象,BinderResult中包含轉(zhuǎn)換后的信息,也包含校驗(yàn)后的錯(cuò)誤信息。
數(shù)據(jù)轉(zhuǎn)換
在java語(yǔ)言中,在java.beans包中提供了一個(gè)PropertyEditor接口來(lái)進(jìn)行數(shù)據(jù)轉(zhuǎn)換,PropertyEditor的核心功能是將一個(gè)String轉(zhuǎn)換為一個(gè)java對(duì)象。Spring從3.0開(kāi)始添加一個(gè)通用的類(lèi)型轉(zhuǎn)換模塊即為org.springframework.convert包中,ConversionService是org.springframework.convert包的核心組件,可以通過(guò)使用ConversionServiceFactoryBean在spring的上下文中自定義一個(gè)ConversionService,Spring將自動(dòng)識(shí)別這個(gè)ConversionService,并在SpringMVC進(jìn)行參數(shù)轉(zhuǎn)換時(shí)使用,配置例子如下所示:
1
2
3
4
5
6
7
8
|
<bean id= "conversionService" class = "org.springframework.context.support.ConversionServiceFactoryBean" > <property name= "converters" > <list> <bean class = "org.xx..StringToDateConverter" /> </list> </property> </bean> |
SpringMVC在支持新的轉(zhuǎn)換器框架的同時(shí),也支持javabeans的PropertyEditor,可以在控制器中使用@InitBinder添加自定義的編輯器。
舉例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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.查詢(xún)通過(guò)@InitBinder自定義的編輯器;
2.查詢(xún)通過(guò)ConversionService裝配的自定義轉(zhuǎn)換器;
3.查詢(xún)通過(guò)WebBindingInitializer接口裝配的全局自定義編輯器。
Formater
除了org.springframework.core.convert.converter接口中定義的三種類(lèi)型的轉(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的使用示例如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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包中定義了兩個(gè)注解,@DateTimeFormat和@NumberFormat 這兩個(gè)注解可以用在domain中的屬性上,SpringMVC處理方法參數(shù)綁定數(shù)據(jù)、模型數(shù)據(jù)輸出時(shí)會(huì)自動(dòng)通過(guò)注解應(yīng)用格式化的功能。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://segmentfault.com/a/1190000011340970