zoukankan      html  css  js  c++  java
  • BeanUtils(前端赋值给后台,忽略空属性)

    package com.drn.core.util;
    
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.FatalBeanException;
    import org.springframework.util.Assert;
    
    public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
    
    	public static void copyProperties(Object source, Object target) throws BeansException {
    		Assert.notNull(source, "Source must not be null");
    		Assert.notNull(target, "Target must not be null");
    		Class<?> actualEditable = target.getClass();
    		PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    		for (PropertyDescriptor targetPd : targetPds) {
    			if (targetPd.getWriteMethod() != null) {
    				PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
    				if (sourcePd != null && sourcePd.getReadMethod() != null) {
    					try {
    						Method readMethod = sourcePd.getReadMethod();
    						if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
    							readMethod.setAccessible(true);
    						}
    						Object value = readMethod.invoke(source);
    						// 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
    						if (value != null) {
    							boolean isEmpty = false;
    							if (value instanceof Set) {
    								Set s = (Set) value;
    								if (s == null || s.isEmpty()) {
    									isEmpty = true;
    								}
    							} else if (value instanceof Map) {
    								Map m = (Map) value;
    								if (m == null || m.isEmpty()) {
    									isEmpty = true;
    								}
    							} else if (value instanceof List) {
    								List l = (List) value;
    								if (l == null || l.size() < 1) {
    									isEmpty = true;
    								}
    							} else if (value instanceof Collection) {
    								Collection c = (Collection) value;
    								if (c == null || c.size() < 1) {
    									isEmpty = true;
    								}
    							}
    
    							if (!isEmpty) {
    								Method writeMethod = targetPd.getWriteMethod();
    								if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
    									writeMethod.setAccessible(true);
    								}
    								writeMethod.invoke(target, value);
    							}
    						}
    					} catch (Throwable ex) {
    						throw new FatalBeanException("Could not copy properties from source to target", ex);
    					}
    				}
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    数位dp模板
    HDU
    hdu 2586 How far away ? ( 离线 LCA , tarjan )
    POJ 1655 Balancing Act( 树的重心 )
    HDU 2196 Computer( 树上节点的最远距离 )
    HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
    HDU 5265 pog loves szh II
    2015区域赛起航
    2015GDCPC广东省赛总结
    SGU 521 North-East ( 二维LIS 线段树优化 )
  • 原文地址:https://www.cnblogs.com/durenniu/p/10867689.html
Copyright © 2011-2022 走看看