spring有内置的属性编辑器,但对date类型不支持..需要自己写个属性编辑器..
1.属性编辑器如下:
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format="yyyy-MM-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
2.将属性编辑器注册一下.
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.wp.spring.UtilDatePropertyEditor"/>
</entry>
</map>
</property>
</bean>
Spring自定义Date属性编辑器
本文介绍如何在Spring中自定义属性编辑器以支持日期类型的解析与设置,包括实现自定义编辑器类及将其注册到Spring上下文的方法。
1132

被折叠的 条评论
为什么被折叠?



