Ejemplo n.º 1
0
            @Override
            public Object convert(String value, Class clazz) {
              if (clazz.isEnum()) {
                return Enum.valueOf(clazz, value);
              } else if (clazz.equals(Date.class)) {
                Date date = null;
                if (Validators.isDate(value)) {
                  date = DateUtils.string2Date(value);
                } else if (Validators.isDateTime(value)) {
                  date = DateUtils.string2DateTime(value);
                }

                return date == null ? value : date;
              } else {
                return super.convert(value, clazz);
              }
            }
Ejemplo n.º 2
0
  /**
   * 鎸夌収鍙傛暟Map璁剧疆瀵硅薄鐨勫睘鎬х殑锟�
   *
   * @param bean 瀵硅薄
   * @param parameters 鍙傛暟
   */
  public static void setProperties(Object bean, Map parameters) {
    // try {
    // BeanUtils.populate(bean, parameters);
    // }
    // catch (Exception e) {
    // throw new RuntimeException("Could not setProperties", e);
    // }

    // Do nothing unless both arguments have been specified
    if ((bean == null) || (parameters == null)) {
      return;
    }

    // Loop through the property name/value pairs to be set
    Iterator iterator = parameters.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = (Map.Entry) iterator.next();

      // Identify the property name and value(s) to be assigned
      String name = (String) entry.getKey();
      if (name == null) {
        continue;
      }
      Object value = entry.getValue();

      // Perform the assignment for this property
      boolean isSuccess = true;
      try {
        BeanUtils.setProperty(bean, name, value);
      } catch (Exception e) {
        isSuccess = false;
      }

      if (!isSuccess) {
        if (value instanceof String) { // 瀛楃涓茶ˉ鍏呭锟�
          String string = ((String) value).trim();
          Date date = null;

          // 锟�瑕佸垽鏂棩鏈燂拷?锟芥椂闂存槸鍚︾鍚堟牸锟�
          if (Validators.isDate(string)) {
            date = DateUtils.string2Date(string);
          } else if (Validators.isDateTime(string)) {
            date = DateUtils.string2DateTime(string);
          }

          if (date == null) {
            continue;
          }

          try {
            BeanUtils.setProperty(bean, name, date);
            isSuccess = true;
          } catch (Exception e) {
          }

          if (!isSuccess) {
            try {
              BeanUtils.setProperty(bean, name, new String[] {(String) value});
            } catch (Exception e) {
            }
          }
        } else { // 鏂囦欢琛ュ厖澶勭悊
          try {
            BeanUtils.setProperty(bean, name, new FileItem[] {(FileItem) value});
          } catch (Exception e) {
          }
        }
      }
    }
  }