Package org.springframework.validation

Examples of org.springframework.validation.DataBinder


  }

  @Test
  public void testPlaceholdersErrorInNonEnumerable() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new PropertySource<Object>("application", "STUFF") {
      @Override
      public Object getProperty(String name) {
        return new Object();
      }
    });
    binder.bind(new PropertySourcesPropertyValues(this.propertySources,
        (Collection<String>) null, Collections.singleton("name")));
    assertEquals(null, target.getName());
  }
View Full Code Here


  }

  // 从requestValue创建Attribute
  private Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter,
          WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
      TypeDescriptor source = TypeDescriptor.valueOf(String.class);
      TypeDescriptor target = new TypeDescriptor(parameter);
      if (conversionService.canConvert(source, target)) {
        return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
      }
    }
    return null;
  }
View Full Code Here

          mandatoryValues.add(field.getName());
        }
      }
    }

    DataBinder binder = new DataBinder(component) {
      @Override
      protected void checkRequiredFields(MutablePropertyValues mpvs) {
        String[] requiredFields = getRequiredFields();
        if (!ObjectUtils.isEmpty(requiredFields)) {
          Map<String, PropertyValue> propertyValues = new HashMap<String, PropertyValue>();
          PropertyValue[] pvs = mpvs.getPropertyValues();
          for (PropertyValue pv : pvs) {
            String canonicalName = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
            propertyValues.put(canonicalName, pv);
          }
          for (String field : requiredFields) {
            PropertyValue pv = propertyValues.get(field);
            boolean empty = (pv == null || pv.getValue() == null);
            // For our purposes, empty Strings or empty String arrays do not count as
            // empty. Empty is only "null".
            // if (!empty) {
            // if (pv.getValue() instanceof String) {
            // empty = !StringUtils.hasText((String) pv.getValue());
            // }
            // else if (pv.getValue() instanceof String[]) {
            // String[] values = (String[]) pv.getValue();
            // empty = (values.length == 0 || !StringUtils.hasText(values[0]));
            // }
            // }
            if (empty) {
              // Use bind error processor to create FieldError.
              getBindingErrorProcessor()
                      .processMissingFieldError(field, getInternalBindingResult());
              // Remove property from property values to bind:
              // It has already caused a field error with a rejected value.
              if (pv != null) {
                mpvs.removePropertyValue(pv);
                propertyValues.remove(field);
              }
            }
          }
        }
      }
    };
    binder.initDirectFieldAccess();
    PropertyEditorUtil.registerUimaFITEditors(binder);
    binder.setRequiredFields(mandatoryValues.toArray(new String[mandatoryValues.size()]));
    binder.bind(values);
    if (binder.getBindingResult().hasErrors()) {
      StringBuilder sb = new StringBuilder();
      sb.append("Errors initializing [" + component.getClass() + "]");
      for (ObjectError error : binder.getBindingResult().getAllErrors()) {
        if (sb.length() > 0) {
          sb.append("\n");
        }
        sb.append(error.getDefaultMessage());
      }
View Full Code Here

   * @param response the response
   * @return a selection of the "new reward" view with the data needed to render it
   */
  public ModelAndView newForm(HttpServletRequest request, HttpServletResponse response) {
    RewardForm rewardForm = new RewardForm();
    DataBinder binder = new DataBinder(rewardForm, "rewardForm");
    // installs a type converter to convert from String to MonetaryAmount during form data binding
    binder.registerCustomEditor(MonetaryAmount.class, new MonetaryAmountEditor());
    return new ModelAndView("reward/new", binder.getBindingResult().getModel());
  }
View Full Code Here

   * @param response the response
   * @return a selection of the "show" view to render the reward
   */
  public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
    Reward reward = rewardLookupService.lookupReward(extractConfirmationNumber(request));
    DataBinder binder = new DataBinder(reward, "reward");
    binder.registerCustomEditor(MonetaryAmount.class, new MonetaryAmountEditor());
    binder.registerCustomEditor(SimpleDate.class, new SimpleDateEditor());
    return new ModelAndView("reward/show", binder.getBindingResult().getModel());
  }
View Full Code Here

TOP

Related Classes of org.springframework.validation.DataBinder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.