Esempio n. 1
0
  public static void copyModeltoModel(Object src, Object des) {
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(des); // 得到to对象的字段属性集合
    for (int i = 0; i < pds.length; i++) {
      try {
        // 得到to对象的属性的类型
        String toClassName = ClassUtils.getShortClassName(pds[i].getPropertyType());
        // 根据此字段名去from对象查找 如果找不到则跳过 继续下一个to字段的赋值
        PropertyDescriptor descriptor = null;
        try {
          descriptor = PropertyUtils.getPropertyDescriptor(src, pds[i].getDisplayName());
          if (descriptor == null) {
            continue; // Skip this property setter
          }
        } catch (NoSuchMethodException e) {
          continue; // Skip this property setter
        }
        // 得到form对象该字段的类型
        String fromClassName = ClassUtils.getShortClassName(descriptor.getPropertyType());

        if ("Class".equals(toClassName)) {
          continue;
        } else {
          // 得到form字段该字段的值 如果有值则赋值 否则继续下一个
          // System.out.println("\n\n\n pds[i].getDisplayName()=" +
          // pds[i].getDisplayName() + "\n\n\n");
          Object o = PropertyUtils.getProperty(src, pds[i].getDisplayName());
          if (o != null) {
            if (toClassName.equals("Date") && fromClassName.equals("String")) { // 如果form该字段是字符串
              // o = Helper.switchDate((String) o);
              // BeanUtils.setProperty(des,
              // pds[i].getDisplayName(), o);
              ConvertUtilsBean convertUtils = new ConvertUtilsBean();
              convertUtils.register(new DateConverter(), Date.class);
              BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils, new PropertyUtilsBean());
              o = Helper.switchDate((String) o);
              beanUtils.setProperty(des, pds[i].getDisplayName(), o);
            } else if (toClassName.equals("String") && fromClassName.equals("Date")) {
              o = DateUtil.toStringByFormat((Date) o, "yyyy-MM-dd HH:mm:ss");
              if (org.apache.commons.lang.StringUtils.contains((String) o, "00:00:00")) {
                o =
                    org.apache.commons.lang.StringUtils.substringBeforeLast((String) o, "00:00:00")
                        .trim();
              }
              org.apache.commons.beanutils.BeanUtils.setProperty(des, pds[i].getDisplayName(), o);
            } else if (toClassName.equals("Date") && fromClassName.equals("Date")) {
              ConvertUtilsBean convertUtils = new ConvertUtilsBean();
              convertUtils.register(new DateConverter(), Date.class);
              BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils, new PropertyUtilsBean());
              beanUtils.setProperty(des, pds[i].getDisplayName(), o);
              // BeanUtils.setProperty(des,
              // pds[i].getDisplayName(), o);
            } else {
              org.apache.commons.beanutils.BeanUtils.setProperty(des, pds[i].getDisplayName(), o);
            }
            // org.apache.commons.beanutils.BeanUtils.setProperty(des,pds[i].getDisplayName(),o);
          }
        }
      } catch (Exception ex) {
        // System.out.println("赋值失败:" + ex);
      }
    }
  }