深入理解Spring MVC的數(shù)據(jù)轉(zhuǎn)換
本文主要給大家介紹了關(guān)于Spring MVC數(shù)據(jù)轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。
數(shù)據(jù)綁定
SpringMVC負責將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組件進行數(shù)據(jù)的校驗工作;
5.經(jīng)歷以上步驟后,DataBinder將生成BinderResult對象,BinderResult中包含轉(zhuǎn)換后的信息,也包含校驗后的錯誤信息。
數(shù)據(jù)轉(zhuǎn)換
在java語言中,在java.beans包中提供了一個PropertyEditor接口來進行數(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進行參數(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實現(xiàn)的是object到object的轉(zhuǎn)換,而format實現(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í)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
java實現(xiàn)簡單學(xué)生成績檔案管理系統(tǒng)
這篇文章主要為大家詳細介紹了java實現(xiàn)簡單學(xué)生成績檔案管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05解決springboot 2.x集成log4j2調(diào)試日志無法關(guān)閉的問題
這篇文章主要介紹了解決springboot 2.x集成log4j2調(diào)試日志無法關(guān)閉的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析一之零配置文件化
Dwr對我來說最重要的功能點就是反向Ajax調(diào)用,通俗來將就是后端可以直接調(diào)用前端的JS方法(只要在所能訪問的范圍內(nèi)),這也就是Dwr的真正來由,當然它也有最基本的前端直接調(diào)用后端的特性,省去了我們經(jīng)常的一般Ajax調(diào)用2016-04-04Spring學(xué)習(xí)通過AspectJ注解方式實現(xiàn)AOP操作
這篇文章主要為大家介紹了Spring學(xué)習(xí)通過AspectJ注解方式實現(xiàn)AOP操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05