zoukankan      html  css  js  c++  java
  • 30天轻松掌握JavaWeb_使用beanutils

    导入commons-beanutils-1.8.3.jar及commons-logging-1.1.3.jar

    使用commons-beanutils-1.8.3.jar包时需要同时使用commons-logging-1.1.3.jar包,因为需要写日志。

    package com.wzh.test.beanutils;
    
    import java.lang.reflect.InvocationTargetException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.ConversionException;
    import org.apache.commons.beanutils.ConvertUtils;
    import org.apache.commons.beanutils.Converter;
    import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
    import org.junit.Assert;
    import org.junit.Test;
    
    //使用BeanUtils操作Bean属性(第三方)
    public class Demo1 {
    
        @Test
        public void test1() throws IllegalAccessException,
                InvocationTargetException {
            Person p = new Person();
            BeanUtils.setProperty(p, "name", "victor");
    
            System.out.println("name:"+p.getName());
        }
    
        @Test
        public void test2() throws IllegalAccessException,
                InvocationTargetException {
            Person p = new Person();
            // 自动将String转为int 支持8种基本数据类型
            BeanUtils.setProperty(p, "age", "23");
            // 默认不支持时间转换
            BeanUtils.setProperty(p, "Birthday", "2012-3-1");
            System.out.println("age:"+p.getAge());
            System.out.println("birthday:"+p.getBirthday());
        }
    
        @Test
        public void test3() throws IllegalAccessException,
                InvocationTargetException {
    
            // 为了让日期赋到Bean的Birthday属性上,我们给BeanUtils注册一个日期转换器
            // 方法1.
            ConvertUtils.register(new Converter() {
                @Override
                public Object convert(Class type, Object value) {
                    if (value == null)
                        return null;
    
                    if (!(value instanceof String)) {
                        System.out.println("不是日期类型");
                        throw new ConversionException("不是日期类型");
                    }
    
                    String str = (String) value;
                    if (str.trim().equals("")) {
                        return null;
                    }
    
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        return df.parse(str);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);// 异常链不能断
                    }
                }
            }, Date.class);
    
            // ConvertUtils.register(new DateLocaleConverter(), Date.class);
            Person p = new Person();
            // 自动将String转为int 支持8种基本数据类型
    
            BeanUtils.setProperty(p, "birthday", "2012-12-12");
            System.out.println("birthday2:"+p.getBirthday());
        }
    
        @Test
        public void test4() throws IllegalAccessException,
                InvocationTargetException {
    
            Map map = new HashMap();
            map.put("name", "aa");
            map.put("birthday", "2012-1-1");
            //使用自带的转换器
            ConvertUtils.register(new DateLocaleConverter(), Date.class);
            Person p = new Person();
            // 自动将String转为int 支持8种基本数据类型
    
            BeanUtils.populate(p, map);//用Map集合中的值填充到Bean的属性
            System.out.println(p.getBirthday());
        }
    }
  • 相关阅读:
    Uploader 文件上传
    filters过滤器的使用
    Calendar中遇到的问题
    中科院之旅
    Python基础教程:列表推导式详解
    不会也要知道的,Python四种实现排序的方法
    2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案
    2021字节跳动校招秋招算法面试真题解题报告--leetcode206 反转链表,内含7种语言答案
    求协方差
    国外卡组织的 交换费-interchangefee(发卡行服务费) 和 银联对比
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3731156.html
Copyright © 2011-2022 走看看