bean类:
[html] view plain copy
- package com.zm.bean;
- import java.util.Date;
- public class Bean1 {
- private Date dateValue;
- public Date getDateValue() {
- return dateValue;
- }
- public void setDateValue(Date dateValue) {
- this.dateValue = dateValue;
- }
- }
自定义属性编辑器类(UtilDatePropertyEditor):
[html] view plain copy
- package com.zm.bean;
- import java.beans.PropertyEditorSupport;
- import java.text.SimpleDateFormat;
- import java.text.ParseException;
- import java.util.Date;
- public class UtilDatePropertyEditor extends PropertyEditorSupport {
- private String format="yyyy-MM-dd";
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- // TODO Auto-generated method stub
- System.out.println("UtilDatePropertyEditor.setAsText() -- text=" + text);
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- try{
- Date d = sdf.parse(text);
- this.setValue(d);
- }catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void setFormat(String format) {
- this.format = format;
- }
- }
相关配置:
[html] view plain copy
- <span style="white-space:pre"> </span><!-- 配置bean1 -->
- <bean id="bean1" class="com.zm.bean.Bean1">
- <property name="dateValue" value="2016-08-20"/>
- </bean>
- <!-- 配置自定义属性编辑器 -->
- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
- <property name="customEditors">
- <map>
- <entry key="java.util.Date" >
- <bean class="com.zm.bean.UtilDatePropertyEditor">
- <property name="format" value="yyyy-MM-dd"/>
- </bean>
- </entry>
- </map>
- </property>
- </bean>
时间: 2024-10-15 01:27:48