java提交from表单(用于数据字段很多,不用后台手动赋值)

public static Object convertBenaToBena(Object from,Object to){
  try {
   BeanInfo beanInfo = Introspector.getBeanInfo(to.getClass());
   PropertyDescriptor[] ps = beanInfo.getPropertyDescriptors();
   for (PropertyDescriptor p : ps) {
    Method getMethod = p.getReadMethod();
    Method setMethod = p.getWriteMethod();
    try {
     if (getMethod != null && setMethod != null) {
      Object value = getMethod.invoke(from);
      setMethod.invoke(to, value);
     }
    } catch (Exception e) {
     System.err.println("请写get与set方法");
     continue;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return to;
 }
原文地址:https://www.cnblogs.com/dsking/p/5254363.html