首页 > 代码库 > 技巧分享

技巧分享

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 = http://www.mamicode.com/method.invoke(object, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }

    }

技巧分享