# 1 类型转换
主要用运用:
Converter 接口
FormattingConversionServiceFactoryBean
1.1 配置
实现WebBindingInitializer接口这样一来就无法使用mvc:annotation-driven 了。使用conversion-service来注册自定义的converter
public class CustomerConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
spring配置文件:
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.zhepu.web.customerBinding.CustomerConverter" />
</list>
</property>
<mvc:annotation-driven conversion-service="conversionService" />