bean类:
- package com.zm.bean;
-
- import java.util.Date;
-
- public class Bean1 {
- private Date dateValue;
-
- public Date getDateValue() {
- return dateValue;
- }
-
- public void setDateValue(Date dateValue) {
- this.dateValue = dateValue;
- }
-
- }
自定义属性编辑器类(UtilDatePropertyEditor):
- package com.zm.bean;
-
- import java.beans.PropertyEditorSupport;
- import java.text.SimpleDateFormat;
- import java.text.ParseException;
- import java.util.Date;
-
- public class UtilDatePropertyEditor extends PropertyEditorSupport {
-
- private String format="yyyy-MM-dd";
-
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- // TODO Auto-generated method stub
- System.out.println("UtilDatePropertyEditor.setAsText() -- text=" + text);
-
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- try{
- Date d = sdf.parse(text);
- this.setValue(d);
- }catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public void setFormat(String format) {
- this.format = format;
- }
-
- }
相关配置:
- <span style="white-space:pre"> </span>
- <bean id="bean1" class="com.zm.bean.Bean1">
- <property name="dateValue" value="2016-08-20"/>
- </bean>
-
- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
- <property name="customEditors">
- <map>
- <entry key="java.util.Date" >
- <bean class="com.zm.bean.UtilDatePropertyEditor">
- <property name="format" value="yyyy-MM-dd"/>
- </bean>
- </entry>
- </map>
- </property>
- </bean>
Spring自定义属性编辑器
原文:http://www.cnblogs.com/hanguocai/p/7783047.html