实现自定义类型转换器
一、编写自定义转换类
public class StringToDateConverter implements Converter<String,Date> {
/**
*
* @param //传入进来字符串
* @return
*/
@Override
public Date convert(String str) {
if(str == null){
throw new RuntimeException("请您传入数据");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
// 把字符串转换日期
return df.parse(str);
} catch (Exception e) {
throw new RuntimeException("数据类型转换出现错误");
}
}
}
二、在springmvc.xml进行配置
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.itcast.account.exception.StringToDateConverter"/>
</set>
</property>
</bean>