多语言展示
当前在线:1470今日阅读:26今日分享:39

Spring如何自定义属性编辑器

Spring如何自定义属性编辑器,其实这个问题也不难,步骤如下。
方法/步骤
1

首先,创建一个类继承java.beans.PropertyEditorSupport。重写其方法。setAsText(String Text):public void setAsText(String Text) throws IllegalArgumentException {System.out.println(“UtilDatePropertyEditor.setAsText() text=”+Text);SimpleDateFormat sdf = new SimpleDateFormat(format);try{Date d = sdf.parse(Text);this.setValue(d);}catch(ParseException e){e.printStackTrace();}}

2

然后,再在配置文件中配置:< bean id=”customerEditorConfigure” class=”org.springframework.beans.factory.config.CustomEditorConfigurer”>< property name=”customEditors”>< map>< entry key=”java.util.Date”>< bean class=”com.zhangjie.spring.UtilDatePropertyEditor”>< /entry>< /map>< /property>< /bean>

3

最后在配置文件中配置java.util.Date属性都会将字符串转为时间。< value>2012-10-24< /property>

推荐信息