<bean id="CustomEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.dzc.DatePropertyEditor"> </bean>
</entry>
</map>
</property>
</bean>
packet com.dzc
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport { @Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
} } 报错原因:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomEditorConfigurer' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [DatePropertyEditor] to required type [java.lang.String] for property 'customEditors[java.util.Date]': no matching editors or conversion strategy found
我看了CustomEditorConfigurer类的源码,发现customEditors属性的类型是Map <String,String>,是不是因为取出的Date类型的值无法放入Map的Value内导致的错误,而在spring2.0中运行时正常的,对比了CustomEditorConfigurer源码发现customEditors的Map类型没定义泛型。如果真是这样,那在Spring3.0中怎么写个Date类型的自定义属性编辑器啊,望各位大虾帮帮小弟,偶才刚刚开始学习spring,很多不懂,谢谢大家了。
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.dzc.DatePropertyEditor"> </bean>
</entry>
</map>
</property>
</bean>
packet com.dzc
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport { @Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
} } 报错原因:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomEditorConfigurer' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [DatePropertyEditor] to required type [java.lang.String] for property 'customEditors[java.util.Date]': no matching editors or conversion strategy found
我看了CustomEditorConfigurer类的源码,发现customEditors属性的类型是Map <String,String>,是不是因为取出的Date类型的值无法放入Map的Value内导致的错误,而在spring2.0中运行时正常的,对比了CustomEditorConfigurer源码发现customEditors的Map类型没定义泛型。如果真是这样,那在Spring3.0中怎么写个Date类型的自定义属性编辑器啊,望各位大虾帮帮小弟,偶才刚刚开始学习spring,很多不懂,谢谢大家了。
解决方案 »
- JAVA 网页爬虫小程序
- java学习方向
- <%@ taglib prefix="s" uri="/struts-tags"%>这句报错了
- hibernate 如何外键关联的问题
- jbpm 问题 搞了两天了 没搞定 晕死了 请高手帮忙
- 新手请教Serializable Key怎么使用
- HttpClient中的getResponseBodyAsString停止执行
- 页面加快多表显示速度问题
- (Struts2 + hibernate) 我点一次提交(多条件查询),Action类的查询方法会执行两次?怎么回事?
- 想在服务器上架设一个网站,用tomcat做web服务器,急急急
- 正被机器 '123' 上的用户 'admin' 锁定
- java初中级水平提高方法
我已经解决了这个问题,需要把定义方式改一下,即把
<entry key="java.util.Date">
<bean class="com.dzc.DatePropertyEditor"> </bean>
</entry>
改为
<entry key="java.util.Date" value="com.dzc.DatePropertyEditor"/>
即可搞定!