码迷,mamicode.com
首页 > 编程语言 > 详细

Spring自定义属性编辑器

时间:2017-11-04 14:49:23      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:simple   XML   support   frame   cep   class   name   stack   ring   

bean类:

  1. package com.zm.bean;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Bean1 {  
  6.     private Date dateValue;  
  7.   
  8.     public Date getDateValue() {  
  9.         return dateValue;  
  10.     }  
  11.   
  12.     public void setDateValue(Date dateValue) {  
  13.         this.dateValue = dateValue;  
  14.     }  
  15.       
  16. }  
自定义属性编辑器类(UtilDatePropertyEditor):

  1. package com.zm.bean;  
  2.   
  3. import java.beans.PropertyEditorSupport;  
  4. import java.text.SimpleDateFormat;  
  5. import java.text.ParseException;  
  6. import java.util.Date;  
  7.   
  8. public class UtilDatePropertyEditor extends PropertyEditorSupport {  
  9.       
  10.     private String format="yyyy-MM-dd";  
  11.       
  12.     @Override  
  13.     public void setAsText(String text) throws IllegalArgumentException {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("UtilDatePropertyEditor.setAsText() -- text=" + text);  
  16.           
  17.         SimpleDateFormat sdf = new SimpleDateFormat(format);  
  18.         try{  
  19.             Date d = sdf.parse(text);  
  20.             this.setValue(d);  
  21.         }catch (ParseException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.   
  27.     public void setFormat(String format) {  
  28.         this.format = format;  
  29.     }  
  30.       
  31. }  

 

相关配置:

 

    1. <span style="white-space:pre">    </span><!-- 配置bean1 -->  
    2.     <bean id="bean1" class="com.zm.bean.Bean1">  
    3.          <property name="dateValue" value="2016-08-20"/>  
    4.     </bean>  
    5.     <!-- 配置自定义属性编辑器 -->  
    6.     <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
    7.         <property name="customEditors">  
    8.             <map>  
    9.                 <entry key="java.util.Date" >  
    10.                     <bean class="com.zm.bean.UtilDatePropertyEditor">  
    11.                         <property name="format" value="yyyy-MM-dd"/>  
    12.                     </bean>  
    13.                 </entry>  
    14.             </map>  
    15.         </property>  
    16.     </bean

Spring自定义属性编辑器

标签:simple   XML   support   frame   cep   class   name   stack   ring   

原文地址:http://www.cnblogs.com/hanguocai/p/7783047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!