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. javascript仿天猫加入购物车动画效果

    javascript仿天猫加入购物车动画效果   注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...

  2. 分享一个导航条哈(⊙o⊙)…

    原文:http://www.sharejs.com/js/menu/1601 CSS样式表: <!--[if lt IE 9]> <script src="http://h ...

  3. TYVJ1359 收入计划

    描述     高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天(1<=N<=100 000).每一天末他可以领取当天及前面若干天里没有 ...

  4. Entity Framework Fluent API

    前言 使用DataAnnotation非常简单,但对于EntityFramework中的特性,就要在实体类中引入EntityFramework程序集,但实体类最好能是保持与架构无关性的POCO类,才能 ...

  5. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  6. 前端与php的sublime text3常用插件

    sublime text各种版本下载:http://sublimetext.iaixue.com/dl/ 1.安装Package Control 快捷键: ctrl+`     粘贴内容后Enter: ...

  7. 第16章 使用Squid部署代理缓存服务

    章节概述: 本章节从代理缓存服务的工作原理开始讲起,让读者能够清晰理解正向代理(普通模式.透明模式)与反向代理的作用. 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低 ...

  8. 安装ruby

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 过程中有点小曲折,我们leader是技术大牛,现在我生命中多了个超高智商处女座man了,还有一 ...

  9. MySQL性能优化的最佳经验

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  10. 创建一个最简单的Linux随机启动服务

    转自: http://xiaoxia.org/2011/11/15/create-a-simple-linux-daemon/