各种格式化-BeanUtils mapToBean requestTobean 含有类型转换

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class BeanOperatorUtils {

    private final static SimpleDateFormat sfyyyyMMddHHmmss = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sfyyyyMMdd = new SimpleDateFormat(
            "yyyy-MM-dd");

    public static <T> T request2BeanAndConvertType(HttpServletRequest request,
            Class<T> c) throws InstantiationException, IllegalAccessException,
            InvocationTargetException {
        T o = c.newInstance();
        Field[] fields = c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {

            String fieldName = fields[i].getName();

            //将这改成从map中取值就可以实现,mapTobean的转换

            String value = request.getParameter(fieldName);
            if(null == value) continue;
            Object newValue = toType(value,fields[i].getType());
            //System.out.println("1fieldName="+fieldName+" ; 1value="+value+" ; newValue="+newValue+" ;type="+fields[i].getType());

            if(null == newValue) continue;

            // 将转换后的值转移到bean中

            BeanUtils.copyProperty(    o,fieldName,newValue);
        }
        return o;
    }

    public static void main(String[] args) throws IllegalAccessException,
            InvocationTargetException, InstantiationException {


    }
    //将值得类型进行装换
    public static Object toType(String s, Class c) {
        if (null == s) {
            return null;
        }
        if (c == String.class) {
            return s;
        }
        if (c == int.class) {
            return Integer.parseInt(s);
        }
        if (c == Integer.class) {
            return Integer.valueOf(s);
        }
        if (c == long.class) {
            return Long.parseLong(s);
        }
        if (c == Long.class) {
            return Long.valueOf(s);
        }
        if (c == double.class) {
            return Double.parseDouble(s);
        }
        if (c == Double.class) {
            return Double.valueOf(s);
        }
        if (c == char.class) {
            return s.charAt(0);
        }
        if (c == Date.class) {
            try {
                if (s.length() == 19) {
                    return sfyyyyMMddHHmmss.parse(s);
                }
                if (s.length() == 10) {
                    return sfyyyyMMdd.parse(s);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

  
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值