1.转换类型的定义:StringtoDateConverter
//字符串转换成日期
public class StringtoDateConverter implements Converter<String, Date> {
//传入进来字符串
@Override
public Date convert(String source) {
if(source == null){
throw new RuntimeException("请您传入数据");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try{
//字符串转日期
return df.parse(source);
}catch (Exception e){
throw new RuntimeException("数据转换异常,出现错误");
}
}
}
2.SpringMVC中注册组建,加载组建
<!-- 开启SpringMVC框架注解的支持, -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean" />
<!-- 配置自定义类型转换器-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<!--在SpringMVC中加入自定义组建-->
<property name="converters">
<set>
<bean class="com.joyTop.utils.StringtoDateConverter"></bean>
</set>
</property>
</bean>