還是習(xí)慣問題,之前遇到問題就喜歡查屑那,用過之后就不管了拱镐,結(jié)果沒過多長時間就忘得一干二凈,就像沒有見過一樣持际,今天心情好沃琅,就寫一個剛才看到的,意義對我來說卻不怎么明顯的蜘欲,記錄吧益眉,見到就貼上來。姥份。郭脂。
一、SpringMVC的配置
-
<mvc:annotation-driven />
- 這個配置都干了什么事情呢殿衰?
- 在官方文檔中說這個配置是一個ConversionService朱庆,即這是一個(類型)轉(zhuǎn)換的服務(wù),這個服務(wù)是在控制器的模型需要綁定的時候就會被使用闷祥,即我們在控制器的參數(shù)和前臺傳遞的參數(shù)一致的時候娱颊,或者參數(shù)封裝在一個參數(shù)對象中,SpringMVC會自動把前臺傳遞過來的參數(shù)的值賦值到對應(yīng)的變量上凯砍,而這個賦值之間的數(shù)據(jù)類型轉(zhuǎn)換就是這個配置起作用的箱硕;
- 如果沒有配置,會自動注冊通用的默認(rèn)的格式化器(formatters)和轉(zhuǎn)換器(converters)悟衩。如果你把這個配置加入都SpringMVC的配置中剧罩,那么默認(rèn)的配置將會被@NumberFormat和@DateTimeFormat所取代,并且如果你使用了Joda Time座泳,那么也會安裝對其之完全支持惠昔;
- 自定義自己的格式化器(formatters)和轉(zhuǎn)換器(converters):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven conversion-service="conversionService"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="org.example.MyConverter"/> </set> </property> <property name="formatters"> <set> <bean class="org.example.MyFormatter"/> <bean class="org.example.MyAnnotationFormatterFactory"/> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.example.MyFormatterRegistrar"/> </set> </property> </bean> </beans>
- 至于conversionService的配置可以參考官檔;
- 這個配置都干了什么事情呢殿衰?