W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
數(shù)字的Number
類(lèi)型和日期Date
類(lèi)型的格式化是默認(rèn)安裝了的,包括@NumberFormat
注解和@DateTimeFormat
注解。如果classpath路徑下存在Joda Time依賴(lài),那么完美支持Joda Time的時(shí)間格式化庫(kù)也會(huì)被安裝好。如果要注冊(cè)定制的格式化器或轉(zhuǎn)換器,請(qǐng)覆寫(xiě)addFormatters
方法:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
// Add formatters and/or converters
}
}
使用MVC命名空間時(shí),<mvc:annotation-driven>
也會(huì)進(jìn)行同樣的默認(rèn)配置。要注冊(cè)定制的格式化器和轉(zhuǎn)換器,只需要提供一個(gè)轉(zhuǎn)換服務(wù)ConversionService
:
<?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>
關(guān)于如何使用格式化管理器FormatterRegistrar,請(qǐng)參考 8.6.4 FormatterRegistrar SPI一節(jié),以及
FormattingConversionServiceFactoryBean
的文檔。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: