spring自定義屬性編輯器

spring自定義屬性編輯器
public class UtilDatePropertyEditor extends PropertyEditorSupport {

   private String format= "yyyy-MM-dd";
    
  @Override
   public void setAsText(String text) throws IllegalArgumentException {
    System.out.println( "UtilDatePropertyEditor.saveAsText() -- text=" + text);
    
    SimpleDateFormat sdf = new SimpleDateFormat(format);
     try {
      Date d = sdf.parse(text);
       this.setValue(d);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

   public void setFormat(String format) {
     this.format = format;
  }

}
 
    <!-- 定義屬性編輯器 -->            
   < bean id ="customEditorConfigurer" class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >
     < property name ="customEditors" >
       < map >
         < entry key ="java.util.Date" >
           < bean class ="com.bjsxt.spring.UtilDatePropertyEditor" >
             < property name ="format" value ="yyyy-MM-dd" />
           </ bean >
         </ entry >
       </ map >
     </ property >
   </ bean >  
相關文章
相關標籤/搜索