示例#1
0
  /**
   * 将对象中的属性值置入到fields中。
   *
   * <p>对于<code>isValidated()</code>为<code>true</code>的group,该方法无效。
   */
  public void mapTo(Object object) {
    if (isValidated() || object == null) {
      return;
    }

    if (log.isDebugEnabled()) {
      log.debug(
          "Mapping properties to fields: group=\"{}\", object={}",
          getName(),
          ObjectUtil.identityToString(object));
    }

    BeanWrapper bean = new BeanWrapperImpl(object);
    getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean);

    for (Field field : getFields()) {
      String propertyName = field.getFieldConfig().getPropertyName();

      if (bean.isReadableProperty(propertyName)) {
        Object propertyValue = bean.getPropertyValue(propertyName);
        Class<?> propertyType = bean.getPropertyType(propertyName);
        PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName);

        if (editor == null) {
          editor = BeanUtils.findEditorByConvention(propertyType);
        }

        if (editor == null) {
          if (propertyType.isArray()
              || CollectionFactory.isApproximableCollectionType(propertyType)) {
            field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class));
          } else {
            field.setValue(bean.convertIfNecessary(propertyValue, String.class));
          }
        } else {
          editor.setValue(propertyValue);
          field.setValue(editor.getAsText());
        }
      } else {
        log.debug(
            "No readable property \"{}\" found in type {}",
            propertyName,
            object.getClass().getName());
      }
    }
  }
示例#2
0
  /**
   * 将group中的值置入指定对象。
   *
   * <p>对于<code>isValidated()</code>为<code>false</code>的group,该方法无效。
   */
  public void setProperties(Object object) {
    if (!isValidated() || object == null) {
      return;
    }

    if (isValid()) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Set validated properties of group \""
                + getName()
                + "\" to object "
                + ObjectUtil.identityToString(object));
      }

      BeanWrapper bean = new BeanWrapperImpl(object);
      getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean);

      for (Field field : getFields()) {
        String propertyName = field.getFieldConfig().getPropertyName();

        if (bean.isWritableProperty(propertyName)) {
          PropertyDescriptor pd = bean.getPropertyDescriptor(propertyName);
          MethodParameter mp = BeanUtils.getWriteMethodParameter(pd);
          Object value = field.getValueOfType(pd.getPropertyType(), mp, null);

          bean.setPropertyValue(propertyName, value);
        } else {
          log.debug(
              "No writable property \"{}\" found in type {}",
              propertyName,
              object.getClass().getName());
        }
      }
    } else {
      throw new InvalidGroupStateException("Attempted to call setProperties from an invalid input");
    }
  }