美文网首页
使用PropertyEditor转换数据 Day25 2018-

使用PropertyEditor转换数据 Day25 2018-

作者: Ernest_Chou | 来源:发表于2018-12-15 22:54 被阅读0次

    3. 使用PropertyEditor转换数据

    PropertyEditor是属性编辑器的接口,它规定了将外部设置值转换为内部JavaBean属性值的转换接口方法。PropertyEditor主要的接口方法说明如下:

    • Object getValue():返回属性的当前值。基本类型被封装成对应的包装类实例;
    • void setValue(Object newValue):设置属性的值,基本类型以包装类传入(自动装箱);
    • String getAsText():将属性对象用一个字符串表示,以便外部的属性编辑器能以可视化的方式显示。缺省返回null,表示该属性不能以字符串表示;
    • void setAsText(String text):用一个字符串去更新属性的内部值,这个字符串一般从外部属性编辑器传入;
    • String[] getTags():返回表示有效属性值的字符串数组(如boolean属性对应的有效Tagtruefalse),以便属性编辑器能以下拉框的方式显示出来。缺省返回null,表示属性没有匹配的字符值有限集合;
    • String getJavaInitializationString():为属性提供一个表示初始值的字符串,属性编辑器以此值作为属性的默认值。

    3.1 自定义属性编辑器继承PropertyEditorSupport

    //自定义属性编辑器
    public class DateEditor extends PropertyEditorSupport {
    
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = dateFormat.parse(text);
                setValue(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    

    3.2 使用@InitBinder在控制层中初始化编辑器

    在Controller层增加

     // 在控制器初始化时注册属性编辑器
         @InitBinder
          public void initBinder(WebDataBinder binder){
            // 注册自定义编辑器
            binder.registerCustomEditor(Date.class, new DateEditor());
          }
    

    3.3 利用CustomEditorConfigurer类配置初始化

    CustomEditorConfigurer 类 用于实现在Spring 中注册自己定义的编辑器 。它是Spring 当中一个非常有用的工厂后处理类(工厂后处理通过SpringBeanFactoryPostProcessor 接口实现, 它是在Spring容器启动并初始化之后进行对Spring 容器的操作类)。在Spring中已经注册了不少编辑器类,他们都用于String 类型转换为其他的数据类型,如URL,Date等。

    配置CustomEditorConfigurer 类:

    CustomEditorConfigurer类中有一个customEditor属性,它是一个Map 类型。通过配置它便实现了自定义的编辑器注册。这个Map 的键值对对应着转换类型和编辑器(转换类型是Key,编辑器是Value)。

    自定义编辑器可以简化Spring 的装配Bean。使其更加的简单。不容易发生配置错误。 PS:如果使用SpringApplicationContext容器,那么只需在Spring 的配置文件中进行简单的装配,而对于Bean 工厂可能需要手动的注册才能使用。

    //自定义属性编辑器
    public class DateEditor extends PropertyEditorSupport {
         private String datePattern;
    
        public String getDatePattern() {
            return datePattern;
        }
    
        public void setDatePattern(String datePattern) {
            this.datePattern = datePattern;
        }
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat dateFormat = new SimpleDateFormat(        this.datePattern = datePattern;
    );
            try {
                Date date = dateFormat.parse(text);
                setValue(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    • spring 配置
    <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
          <property name="customEditors">
            <map>
              <entry key="java.util.Date">
                <bean class="com.zhougl.web.converter.DateEditor" />
            </entry>
          </map>
        </property>
      </bean> 
    
        <bean id="dateEditor" class="com.zhougl.web.converter.DateEditor">
          <property name="datePattern" value="yyyy-MM-dd" />
        </bean>
    
    • 测试类
    public class TestMain {
    
        public static void main(String ai[]) {
    
            XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("/applicationcontext.xml"));
    
            /*CustomEditorConfigurer是Spring提供的BeanFactoryProcessor的一个实现,专门用来搞类型转换的*/
            CustomEditorConfigurer configurer = new CustomEditorConfigurer();
    
            DateEditor de = new DateEditor();
            de.setDatePattern("yyyy-MM-dd");
    
            Map customerEditors = new HashMap();
            customerEditors.put("java.util.Date", de);
    
            configurer.setCustomEditors(customerEditors);
            configurer.postProcessBeanFactory(beanFactory);
    
            Entity e = (Entity)beanFactory.getBean("User");
            System.out.println(e.getDate());
    
        }
    }
    

    相关文章

      网友评论

          本文标题:使用PropertyEditor转换数据 Day25 2018-

          本文链接:https://www.haomeiwen.com/subject/aexzhqtx.html