你必須實現自己的org.springframework.format.Formatter
例
public class DateFormatter implements Formatter<Date> {
public String print(Date property, Locale locale) {
//your code here for display
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, LocaleContextHolder.getLocale());
String out = df.format(date);
return out;
}
public Date parse(String source, Locale locale)
// your code here to parse the String
}
}
在Spring配置:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
<property name="formatterRegistrars">
<set>
</set>
</property>
<property name="converters">
<set>
</set>
</property>
<property name="formatters">
<set>
<bean class="com.example.DateFormatter" />
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
希望它可以幫助你!