一、概述
作用:http请求的表单都是String,如果业务参数是基本类型和String,可以通过HandlerAdapter自动转换;但是其他类型Date需要实现Converter
二、实现
1.实现接口Converter
public class DateConverter implements Converter<String, Date> { private String dateFormat; public DateConverter(String dateFormat) { this.dateFormat = dateFormat; } @Override public Date convert(String convertParam) { SimpleDateFormat format = new SimpleDateFormat(dateFormat); try { return format.parse(convertParam); } catch (ParseException e) { e.printStackTrace(); } return null; } }
2.配置注册自定义转化器
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.owl.converter.DateConverter"> <constructor-arg type="java.lang.String" value="yyyyMMdd"/> </bean> </list> </property> </bean> <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
3.测试