标签:style blog io ar color sp for java on
自定义类型转换器(例子是转换的Date类型)
1、新建类型转换类DateConvert.java并继承java.beans.PropertyEditorSupport
2、重写setAsText(String text)方法
public class DateConvert extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse(text); this.setValue(date); } catch (ParseException e) { e.printStackTrace(); } } }
3、在配置文件中配置类型转换器
<bean name="customEditor" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors" > <map> <entry key="java.util.Date" value="com.tidus.spring.util.DateConvert" /> </map> </property> </bean>
4、然后就可以在model中注入Date类型了
<bean name="studentService" class="com.tidus.spring.service.StudentService"> <property name="dao" ref="studentDao" /> <property name="studentList"> <list> <bean name="s1" class="com.tidus.spring.model.Student" > <property name="name" value="张三" /> <property name="id" value="0" /> <property name="age" value="20" /> <property name="birthday" value="1987-05-08" /> </bean> <bean name="s2" class="com.tidus.spring.model.Student" > <property name="name" value="张4" /> <property name="id" value="1" /> <property name="age" value="22" /> <property name="birthday" value="1985-03-08" /> </bean> <bean name="s3" class="com.tidus.spring.model.Student" > <property name="name" value="张5" /> <property name="id" value="2" /> <property name="age" value="21" /> <property name="birthday" value="1987-06-08" /> </bean> </list> </property> </bean>
标签:style blog io ar color sp for java on
原文地址:http://www.cnblogs.com/tiduswj/p/4158779.html