bean类

import java.util.Date;  

public class Bean {
private Date date;
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}

配置xml

<bean id="bean" class="com.spring.bean.Bean">
<property name="date" value="2009-11-21"/>
</bean>

spring不能将string转换成date类型,没有匹配的编辑器或者转换机制。如果想实现string转换成Date,有两种解决办法。

使用自定义属性编辑器

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
   String format;
@Override
public void setAsText(String text) throws IllegalArgumentException
{ try
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
       Date date=sdf.parse(text);
this.setValue(date); //把转换后的值传过去
} catch (Exception e)
{
e.printStackTrace();
}
} }

写完编辑器后还需要把编辑器注入到spring中。

    <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<!-- 把值注入到CustomEditorConfigurer的 Map类型的customEditors属性-->
<property name="customEditors">
<map>
<entry key="java.util.Date">
<!-- 内部bean只供自己使用 -->
<bean class="com.spring.util.DatePropertyEditor">
              <property name="format" value="yyyy/MM/dd"></property>
            </bean>
</entry>
</map>
</property>
</bean>

或者

@InitBinder
public void initBinder(WebDataBinder binder) {
  binder.registerCustomEditor(Date.class, "date", new DatePropertyEditor());
}

注册Spring自带的属性编辑器CustomDateEditor

public class DatePropertyEditorRegistrar implements PropertyEditorRegistrar{
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
}

通过在配置文件中将自定义的DatePropertyEditorRegistrar注册进入org.Springframework.beans.factory.config.CustomEditorConfigurer的propertyEditorRegistrars属性中

  <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="datePropertyEditorRegistrar"/>
</list>
</property>
</bean>

Spring的属性编辑器的更多相关文章

  1. (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器

    上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...

  2. (spring-第12回【IoC基础篇】)JavaBean的属性编辑器

    在spring实例化bean的最后阶段,spring利用属性编辑器将配置文件中的文本配置值转换为bean属性的对应值,例如: 代码0011 <bean id="car" cl ...

  3. 属性编辑器,即PropertyEditor-->Spring IoC

    在Spring配置文件里,我们往往通过字面值为Bean各种类型的属性提供设置值:不管是double类型还是int类型,在配置文件中都对应字符串类型的字面值.BeanWrapper填充Bean属性时如何 ...

  4. Spring属性编辑器详解

    1.常见的属性的注入:int,string,list,set,map 2.什么是属性编辑器及作用? (1)将spring配置文件中的字符串转换为相应的java对象 (2)spring内置了一些属性编辑 ...

  5. Spring经常使用属性的注入及属性编辑器

    对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...

  6. Spring中的属性编辑器的使用

    Spring中的属性编辑器的使用 转载自 http://blog.sina.com.cn/s/blog_59ca2c2a0100jxwh.html Struts中用一个类型转换器,在Spring中也有 ...

  7. Spring 学习笔记 数据绑定,校验,BeanWrapper 与属性编辑器

    Spring 数据绑定,校验,BeanWrapper,与属性编辑器 Data Binding 数据绑定(Data binding)非常有用,它可以动态把用户输入与应用程序的域模型(或者你用于处理用户输 ...

  8. spring源码解析之属性编辑器propertyEditor

    异常信息造成此异常的原因bean配置文件调用代码特别说明:异常解决注册springt自带的属性编辑器 CustomDateEditor控制台输出属性编辑器是何时并如何被注册到spring容器中的?查看 ...

  9. SpringMVC类型转换器、属性编辑器

    对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...

随机推荐

  1. 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1

      <白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...

  2. JS待定···

    <select name="selgroup" id="selSendGroup" onchange="selSendGroupTest(thi ...

  3. iOS 时间处理(转)

    NSDate NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate存储的是GMT时间,使用的时候会根据 当 ...

  4. 微信公众号"赞赏"功能来了 觉得不错就给作者打个赏吧

    微信很早以前就开始测试“赞赏”功能了,只是官方还没出公告,近日腾讯科技就发了一篇题为 试试给微信公众号“赞赏” 的文章,算是一个回应吧.微信打赏功能势在遏制公众账号抄袭,鼓励用户创造优质内容,内容付费 ...

  5. Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏

    著作权声明:本文由http://www.cnblogs.com/icker 原创,欢迎转载分享.转载时请保留该声明和作者博客链接,谢谢! 最近使用Unity3d制作的IOS游戏需要加入联网对战功能功能 ...

  6. Procrustes Analysis普氏分析法

    选取N幅同类目标物体的二维图像,并用上一篇博文的方法标注轮廓点,这样就得到训练样本集: 由于图像中目标物体的形状和位置存在较大偏差,因此所得到的数据并不具有仿射不变性,需要对其进行归一化处理.这里采用 ...

  7. error: library dfftpack has Fortran sources but no Fortran compiler found解决方法

    用pip install scipy 时提示 error: library dfftpack has Fortran sources but no Fortran compiler found 解决方 ...

  8. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

    腾讯新浪通过IP地址获取当前地理位置(省份)的接口  腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...

  9. 使用豆瓣的pypi源

    配置文件位置: 1.linux ~/.pip/pip.conf 2.windows %HOME%\pip\pip.ini 配置文件内容:[global] index-url = http://pypi ...

  10. Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...