从context(相当于request)中获取对象

     /**
     * 因为目前没有数组暂不支持数组
     * @param className
     * @param context
     * @return
     */
    public static <T> T getObject(Class<T> className,IPresentationContext context){
        return getObject(className,context,null,null);
    }
    public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat){
        return getObject(className,context,dateFormat,null);
    }
    public static <T> T getObject(Class<T> className,IPresentationContext context,Map<String,Integer> replace){
        return getObject(className,context,null,replace);
    }
    /**
     * 因为目前没有数组暂不支持数组
     * @param className 对象的类型
     * @param context  对request的封装
     * @param dateFormat 日期格式
     * @param replace 有时候我们会用<b>1</b>代表一些值比如通过<b>0</b>代表不通过
     * @return
     */
    public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat,Map<String,Integer> replace){
        T obj = null;
        BeanInfo beanInfo = null;
        try {
            obj = className.newInstance();
            beanInfo = Introspector.getBeanInfo(className);
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IntrospectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PropertyDescriptor[] propertyDescriptorArr = beanInfo.getPropertyDescriptors();
        for(PropertyDescriptor propertyDescriptor : propertyDescriptorArr){
            String fieldName = propertyDescriptor.getName();
            String fieldValue = context.getParameter(fieldName);
            if(StringUtils.isNotEmpty(fieldValue)){
                Class<?> fieldClass = propertyDescriptor.getPropertyType();
                Method method = propertyDescriptor.getWriteMethod();
                try {
                    if(replace.get(fieldValue) != null){
                        method.invoke(obj, replace.get(fieldValue));
                    }else if("java.lang.Integer".equals(fieldClass.getName())||"int".equals(fieldClass.getName())){
                        method.invoke(obj, Integer.parseInt(fieldValue));
                    }else if("java.util.Date".equals(fieldClass.getName())){
                        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
                        method.invoke(obj, sdf.parse(fieldValue));
                    }else if("java.lang.Float".equals(fieldClass.getName()) || "float".equals(fieldClass.getName())){
                        method.invoke(obj, Float.parseFloat(fieldValue));                        
                    }else{
                        method.invoke(obj, fieldValue);                        
                    }
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    if(e instanceof NumberFormatException){
                        
                    }
                }
            }
        }
        return obj;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值