定义类型转换类
实现Converter的接口
public class StringToDateConverter implements Converter<String, Date> {
/**
* @param s 传入的字符串
* @return 字符串转换的日期
*/
@Override
public Date convert(String s) {
//判断是否为空
if (s == null) {
throw new RuntimeException("字符串为空");
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(s);
} catch (ParseException e) {
throw new RuntimeException("数据类型转换错误");
}
}
}
配置springmvc.xml,加载类型转换类
<!--配置自定义数据转换器 -->
<bean id="conversionServices" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters" >
<set>
<bean class="com.iforeverhz.utils.StringToDateConverter"/>
</set>
</property>
</bean>