zoukankan      html  css  js  c++  java
  • BeanUtils组件

    引入jar包(需要引入依赖的日志jar包)

            Person p = new Person();
            p.setName("Daisy");
            p.setAge(12);
            //对象的copy
            Person newP = new Person();
            BeanUtils.copyProperties(newP,p);
    
            //对象属性的拷贝
            BeanUtils.copyProperty(p,"name","juaner");
            BeanUtils.setProperty(p,"age",10);//如果属性不存在,不会报错
    
            //map对象拷贝到对象中,servlet中请求数据封装到parameterMap中
            // 可以用来初始化bean
            Map<String,Object> propMap = new HashMap<String, Object>();
            propMap.put("name","tom");
            propMap.put("age",25);
            propMap.put("email","123@126.com");
            BeanUtils.populate(p,propMap);

    自定义类型转换器:

            //模拟表单提交
            String name="jack";
            int age = 25;
            String birth = "1990-10-10";
            Person p = new Person();
            //注册日期类型转换器,自定义的方式
            ConvertUtils.register(new Converter() {
                @Override
                public Object convert(Class aClass, Object o) {
                    if(aClass != Date.class)
                        return null;
                    if(o == null||"".equals(o.toString().trim()) )
                        return null;
                    try {
                        SimpleDateFormat format = 
                        new SimpleDateFormat("yyyy-mm-dd"); return format.parse(o.toString()); }catch (ParseException e){ throw new RuntimeException(e); } } }, Date.class); BeanUtils.setProperty(p,"name",name); BeanUtils.setProperty(p,"age",age); BeanUtils.setProperty(p,"birth",birth);

    使用BeanUtils组件提供的转换器:

            //模拟表单提交
            String name="jack";
            int age = 25;
            String birth = "1990-10-25";
            //使用组件提供的转换器工具类,不支持空格
            ConvertUtils.register(new DateLocaleConverter(),Date.class);
            Person p = new Person();
            BeanUtils.setProperty(p,"name",name);
            BeanUtils.setProperty(p,"age",age);
            BeanUtils.setProperty(p,"birth",birth);
  • 相关阅读:
    SupoSE 概述 Redmine
    tmux guake
    How to Install and Use Krugle Basic
    use webdevelop tools to delete domain specific cookies for ecommerical cps union effect
    执行hudson daemon的脚本
    python技巧26[python的egg包的安装和制作]
    HOWTO Modular Xorg
    ARC专题:再续 Xcode 4.2 中的Automatic Reference Counting (ARC)
    ARC专题:在Xcode 4.2中加入不支持ARC的源码
    Windows下的.NET+ Memcached安装
  • 原文地址:https://www.cnblogs.com/juaner767/p/5575501.html
Copyright © 2011-2022 走看看