溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

深入理解Spring MVC的數(shù)據(jù)轉(zhuǎn)換

發(fā)布時(shí)間:2020-08-21 13:55:48 來源:腳本之家 閱讀:116 作者:haofengpingjieli 欄目:編程語言

本文主要給大家介紹了關(guān)于Spring MVC數(shù)據(jù)轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

數(shù)據(jù)綁定

SpringMVC負(fù)責(zé)將request中的信息以一定的方式轉(zhuǎn)換并綁定到處理方法的參數(shù)上。整個(gè)過程的處理核心是由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)后的錯(cuò)誤信息。

數(shù)據(jù)轉(zhuǎn)換

在java語言中,在java.beans包中提供了一個(gè)PropertyEditor接口來進(jìn)行數(shù)據(jù)轉(zhuǎn)換,PropertyEditor的核心功能是將一個(gè)String轉(zhuǎn)換為一個(gè)java對象。Spring從3.0開始添加一個(gè)通用的類型轉(zhuǎn)換模塊即為org.springframework.convert包中,ConversionService是org.springframework.convert包的核心組件,可以通過使用ConversionServiceFactoryBean在spring的上下文中自定義一個(gè)ConversionService,Spring將自動識別這個(gè)ConversionService,并在SpringMVC進(jìn)行參數(shù)轉(zhuǎn)換時(shí)使用,配置例子如下所示:

<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添加自定義的編輯器。

舉例如下:

@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包中定義了兩個(gè)注解,@DateTimeFormat和@NumberFormat 這兩個(gè)注解可以用在domain中的屬性上,SpringMVC處理方法參數(shù)綁定數(shù)據(jù)、模型數(shù)據(jù)輸出時(shí)會自動通過注解應(yīng)用格式化的功能。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI