码迷,mamicode.com
首页 > 其他好文 > 详细

技巧分享

时间:2017-05-12 00:07:21      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:exception   string   ret   getter   字段   declare   param   查询   exce   

1、在jsp页面中被禁用的不会传值到后台,例如checkbox select 标签等,解决办法隐藏域

2、将以个对象的属性转为一个map(继承来的字段不能转, 如果为null 则不会放进map中,0 要放入)

/**
     * 将一个类查询方式加入map(属性值为int型时,0时不加入,
     * 属性值为String型或Long时为null和“”不加入)
     * @param obj
     * @return
     */
    public static Map<String, Object> setConditionMap(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (obj == null) {
            return null;
        }
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            if (getValueByFieldName(fieldName, obj) != null)
                map.put(fieldName, getValueByFieldName(fieldName, obj));
        }
        return map;
    }

    /**
     * 根据属性名获取该类此属性的值
     * @param fieldName
     * @param object
     * @return
     */
    private static Object getValueByFieldName(String fieldName, Object object) {
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String getter = "get" + firstLetter + fieldName.substring(1);
        try {
            Method method = object.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(object, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }

    }

技巧分享

标签:exception   string   ret   getter   字段   declare   param   查询   exce   

原文地址:http://www.cnblogs.com/lijun1990/p/6842828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!