Spring的属性编辑器
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的属性编辑器的更多相关文章
- (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器
上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...
- (spring-第12回【IoC基础篇】)JavaBean的属性编辑器
在spring实例化bean的最后阶段,spring利用属性编辑器将配置文件中的文本配置值转换为bean属性的对应值,例如: 代码0011 <bean id="car" cl ...
- 属性编辑器,即PropertyEditor-->Spring IoC
在Spring配置文件里,我们往往通过字面值为Bean各种类型的属性提供设置值:不管是double类型还是int类型,在配置文件中都对应字符串类型的字面值.BeanWrapper填充Bean属性时如何 ...
- Spring属性编辑器详解
1.常见的属性的注入:int,string,list,set,map 2.什么是属性编辑器及作用? (1)将spring配置文件中的字符串转换为相应的java对象 (2)spring内置了一些属性编辑 ...
- Spring经常使用属性的注入及属性编辑器
对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...
- Spring中的属性编辑器的使用
Spring中的属性编辑器的使用 转载自 http://blog.sina.com.cn/s/blog_59ca2c2a0100jxwh.html Struts中用一个类型转换器,在Spring中也有 ...
- Spring 学习笔记 数据绑定,校验,BeanWrapper 与属性编辑器
Spring 数据绑定,校验,BeanWrapper,与属性编辑器 Data Binding 数据绑定(Data binding)非常有用,它可以动态把用户输入与应用程序的域模型(或者你用于处理用户输 ...
- spring源码解析之属性编辑器propertyEditor
异常信息造成此异常的原因bean配置文件调用代码特别说明:异常解决注册springt自带的属性编辑器 CustomDateEditor控制台输出属性编辑器是何时并如何被注册到spring容器中的?查看 ...
- SpringMVC类型转换器、属性编辑器
对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...
随机推荐
- 10.23lamp环境
前序: 查考文章:http://www.cnblogs.com/mchina/archive/2012/11/28/2778779.html http://www.centos.bz/2011/09/ ...
- CSS初学
CSS作用是美化html网页.内联:body 标签里.内嵌:head标签里,可以多次重复使用.p{}:引用时,class引用.#p{}:引用时,id引用超链接的stylea:link 超链接被点前的状 ...
- sql数据库批量替换dedecms内容关键字
之前写了一篇dedecms后台批量替换文章中的关键词,这边我们介绍一下用sql数据库批量替换dedecms内容关键字,当然要求你对数据库比较熟悉,修改前请自行做好备份. 1.更改文章中的内容 upda ...
- [codeforces 325]B. Stadium and Games
[codeforces 325]B. Stadium and Games 试题描述 Daniel is organizing a football tournament. He has come up ...
- poj3295
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10453 Accepted: 3967 Descri ...
- 最长公共子串 NYOJ 36
http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- 用nginx做反向代理来访问防外链图片
用nginx做反向代理来访问防外链图片 女儿的博客从新浪搬到wordpress后,发现原来博客上链接的新浪相册的图片都不能访问了,一年的博客内容,一个个去重新上传图片,修正链接也是个大工程.还是得先想 ...
- nGrinder性能测试平台搭建(LVS压力测试)
1. nGrinder是什么 nGrinder是一个免费的.开放源代码的Web性能测试平台.运行在应用中间件服务器中运行.它由一个控制端和多个代理端组成.通过控制端(浏览器访问)建立测试场景,然后通过 ...
- ntpdate公司测试
[root@i158 ~]# ntpdate -u time.uuwatch.com 9 Jul 11:18:50 ntpdate[853]: no server suitable for synch ...
- 如何在Linux上实现文件系统的自动检查和修复?
Linux文件系统有可能在各种各样的情况下受到损坏,比如系统崩溃.突然断电.磁盘断开,或者文件节点 (i-node)不小心被覆盖等等,因此需要定期检查文件系统,而说到检查和修复Linux文件系统,fs ...