一、编写自定义类型转换器
import org.springframework.core.convert.converter.Converter; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDate implements Converter<String, Date> { @Override public Date convert(String s) { if(s==null){ System.out.println("请输入正确类型"); } DateFormat df=new SimpleDateFormat("yyy-MM-dd"); try { Date parse = df.parse(s); return parse; } catch (ParseException e) { e.printStackTrace(); } return null; } }
二、配置自定义转换类型
<!-- 配置自定义类型转换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.untils.StringToDate"></bean> </set> </property> </bean>
三、开启转换器
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>