在其他对象属性类型一样情况下,只需要创建一个类,再在springmvc.xml中添加配置:
package com.ujiuye.common; import org.springframework.core.convert.converter.Converter; import java.text.SimpleDateFormat; import java.util.Date; public class DateTimeConverter implements Converter<String, Date> { public Date convert(String s) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try{ Date date = sdf.parse(s); return date; }catch (Exception ex){ System.out.println(ex.getMessage()); return null; } } }
<!--全局类型转型器--> <bean id="converter" class="com.ujiuye.common.DateTimeConverter"></bean> <bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="converter"></ref> </set> </property> </bean> <mvc:annotation-driven conversion-service="formattingConversionService"/> <mvc:annotation-driven/>