自定义转换器实现Converter接口
//String是原始类型,Date是要转换的目标类型 public class CustomDateConverter implements
Converter<String, Date> { @Override public Date convert(String source) { try {
return new SimpleDateFormat("yyyy-MM-dd").parse(source); } catch (Exception e)
{ e.printStackTrace(); } return null; } }
配置转换器
配置方式一:针对不使用mvc驱动的
<!--注解适配器 --> <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property> </bean>
<!-- 自定义webBinder --> <bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" /> </bean> <!--
conversionService --> <bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 --> <property name="converters"> <list> <bean
class="cn.itcast.ssm.controller.converter.CustomDateConverter"/> </list>
</property> </bean>
配置方式二:针对使用mvc驱动的
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven> <!-- conversionService --> <bean
id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 --> <property name="converters"> <list> <bean
class="cn.itcast.ssm.controller.converter.CustomDateConverter"/> </list>
</property> </bean>