@InitBinder("systemNotificationForm")
 protected void initBinder(WebDataBinder binder) {
   binder.setValidator(systemNotificationValidator);
   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
   sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
   binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
   binder.registerCustomEditor(
       SystemNotificationType.class, new EnumEditor(SystemNotificationType.class));
   binder.registerCustomEditor(
       SystemNotificationSeverity.class, new EnumEditor(SystemNotificationSeverity.class));
   binder.registerCustomEditor(
       List.class,
       "types",
       new CustomCollectionEditor(List.class) {
         @Override
         protected Object convertElement(Object element) {
           SystemNotificationType type = null;
           if (element != null) {
             try {
               type = SystemNotificationType.valueOf((String) element);
             } catch (Exception ex) {
               log.error(
                   String.format(
                       "Error converting element to SystemNotificationType: %s", element),
                   ex);
             }
           }
           return type;
         }
       });
 }
Пример #2
0
  /** 初始化数据绑定 1. 将所有传递进来的String进行HTML编码,防止XSS攻击 2. 将字段中Date类型转换为String类型 */
  @InitBinder
  protected void initBinder(
      WebDataBinder binder, HttpServletRequest request, HttpServletResponse response) {
    this.request = request;
    this.response = response;
    // 将所有传递进来的String进行HTML编码,防止XSS攻击
    binder.registerCustomEditor(
        String.class,
        new PropertyEditorSupport() {
          @Override
          public void setAsText(String text) {
            setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
          }

          @Override
          public String getAsText() {
            Object value = getValue();
            return value != null ? value.toString() : "";
          }
        });
    binder.registerCustomEditor(
        Date.class,
        new PropertyEditorSupport() {
          @Override
          public void setAsText(String text) {
            setValue(DateUtils.parseDate(text));
          }
        });
  }
Пример #3
0
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(SignatureForm.class, new EnumPropertyEditor(SignatureForm.class));
   binder.registerCustomEditor(
       SignaturePackaging.class, new EnumPropertyEditor(SignaturePackaging.class));
   binder.registerCustomEditor(SignatureLevel.class, new EnumPropertyEditor(SignatureLevel.class));
 }
 @Override
 protected void initBinderInternal(WebDataBinder binder) {
   if (binder.getTarget() instanceof Service36400234Form) {
     super.setValidator(validator);
     binder.setValidator(validator);
   }
 }
Пример #5
0
 // http://baggioback.iteye.com/blog/1753469
 @InitBinder
 protected void dataBinder(WebDataBinder webDataBinder) {
   // 一个用于修剪(trim)String类型的属性编辑器,具有将一个空字符串转化为null值的选项。
   // 默认没有注册,必须由用户在需要的时候自行注册
   webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
   webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
 }
Пример #6
0
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   binder.setDisallowedFields(new String[] {"studentMobile"});
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy****mm****dd");
   binder.registerCustomEditor(Date.class, "studentDOB", new CustomDateEditor(dateFormat, false));
   binder.registerCustomEditor(String.class, "studentName", new StudentNameEditor());
 }
 @InitBinder
 protected void initBinder(WebDataBinder binder) {
   LazyBindingErrorProcessor errorProcessor = new LazyBindingErrorProcessor();
   binder.setValidator(
       new SavingsProductFormValidator(errorProcessor, configurationServiceFacade));
   binder.setBindingErrorProcessor(errorProcessor);
 }
Пример #8
0
  private Object resolveCookieValue(
      String cookieName,
      boolean required,
      String defaultValue,
      MethodParameter methodParam,
      NativeWebRequest webRequest,
      Object handlerForInitBinderCall)
      throws Exception {

    Class<?> paramType = methodParam.getParameterType();
    if (cookieName.length() == 0) {
      cookieName = getRequiredParameterName(methodParam);
    }
    Object cookieValue = resolveCookieValue(cookieName, paramType, webRequest);
    if (cookieValue == null) {
      if (StringUtils.hasText(defaultValue)) {
        cookieValue = resolveDefaultValue(defaultValue);
      } else if (required) {
        raiseMissingCookieException(cookieName, paramType);
      }
      cookieValue = checkValue(cookieName, cookieValue, paramType);
    }
    WebDataBinder binder = createBinder(webRequest, null, cookieName);
    initBinder(handlerForInitBinderCall, cookieName, binder, webRequest);
    return binder.convertIfNecessary(cookieValue, paramType, methodParam);
  }
Пример #9
0
  @SuppressWarnings("unchecked")
  private Object resolveRequestHeader(
      String headerName,
      boolean required,
      String defaultValue,
      MethodParameter methodParam,
      NativeWebRequest webRequest,
      Object handlerForInitBinderCall)
      throws Exception {

    Class<?> paramType = methodParam.getParameterType();
    if (Map.class.isAssignableFrom(paramType)) {
      return resolveRequestHeaderMap((Class<? extends Map>) paramType, webRequest);
    }
    if (headerName.length() == 0) {
      headerName = getRequiredParameterName(methodParam);
    }
    Object headerValue = null;
    String[] headerValues = webRequest.getHeaderValues(headerName);
    if (headerValues != null) {
      headerValue = (headerValues.length == 1 ? headerValues[0] : headerValues);
    }
    if (headerValue == null) {
      if (StringUtils.hasText(defaultValue)) {
        headerValue = resolveDefaultValue(defaultValue);
      } else if (required) {
        raiseMissingHeaderException(headerName, paramType);
      }
      headerValue = checkValue(headerName, headerValue, paramType);
    }
    WebDataBinder binder = createBinder(webRequest, null, headerName);
    initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
    return binder.convertIfNecessary(headerValue, paramType, methodParam);
  }
Пример #10
0
 /**
  * Method which will be used for populating command and form object arguments.
  *
  * @param binder Binder for data binding from web request parameters to JavaBean objects.
  */
 @InitBinder
 protected void initBinder(WebDataBinder binder) {
   SimpleDateFormat dobFormat = new SimpleDateFormat("MM/dd/yyyy");
   binder.registerCustomEditor(Date.class, "dateOfBirth", new CustomDateEditor(dobFormat, true));
   binder.registerCustomEditor(Date.class, "startDate", new CustomDateEditor(dobFormat, true));
   binder.registerCustomEditor(Date.class, "endDate", new CustomDateEditor(dobFormat, true));
 }
Пример #11
0
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH); // dd/MM/yyyy
   CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
   binder.registerCustomEditor(Date.class, editor);
   binder.registerCustomEditor(Family.class, familyEditor);
 }
Пример #12
0
 @InitBinder
 public void dataBinding(WebDataBinder binder) {
   binder.addValidators(feriasValidation);
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   dateFormat.setLenient(false);
   binder.registerCustomEditor(Date.class, "dataInicio", new CustomDateEditor(dateFormat, true));
 }
Пример #13
0
 /** Register custom, context-specific property editors */
 @InitBinder
 public void initBinder(
     WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
   binder.registerCustomEditor(
       java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
   binder.registerCustomEditor(
       byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
   binder.registerCustomEditor(
       boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
   binder.registerCustomEditor(
       Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
   binder.registerCustomEditor(
       java.math.BigDecimal.class,
       new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(
           java.math.BigDecimal.class, true));
   binder.registerCustomEditor(
       Integer.class,
       new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
   binder.registerCustomEditor(
       java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
   binder.registerCustomEditor(
       String.class, new org.skyway.spring.util.databinding.StringEditor());
   binder.registerCustomEditor(
       Long.class,
       new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
   binder.registerCustomEditor(
       Double.class,
       new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
 }
Пример #14
0
 @InitBinder
 protected void initBinder(WebDataBinder binder) {
   if (binder.getTarget() instanceof User) {
     binder.setValidator(registerValidator);
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
   }
 }
 @InitBinder
 public void initBinder(WebDataBinder binder, @PathVariable("hotel") String hotel) {
   assertEquals("Invalid path variable value", "42", hotel);
   binder.initBeanPropertyAccess();
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   dateFormat.setLenient(false);
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
 }
Пример #16
0
 @Override
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(Driver.class, new AbstractModelEditor(Driver.class));
   binder.registerCustomEditor(FuelCard.class, new AbstractModelEditor(FuelCard.class));
   binder.registerCustomEditor(FuelVendor.class, new AbstractModelEditor(FuelVendor.class));
   binder.registerCustomEditor(Long.TYPE, new CustomNumberEditor(Long.class, false));
 }
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(
       Long.class,
       null,
       new CustomNumberEditor(Long.class, NumberFormat.getNumberInstance(), true));
   binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
 }
Пример #18
0
  @Override
  public void initBinder(WebDataBinder binder, WebRequest request) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
  }
 public WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName)
     throws Exception {
   LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
   validator.afterPropertiesSet();
   WebDataBinder dataBinder = new WebDataBinder(target, objectName);
   dataBinder.setValidator(validator);
   return dataBinder;
 }
Пример #20
0
  @InitBinder
  public void initBinder(WebDataBinder binder) {

    binder.registerCustomEditor(MediaType.class, new MediaTypeEditor(MediaType.class));
    binder.registerCustomEditor(
        String.class,
        new StringTrimmerEditor(
            true)); /* Converts empty strings into null when a form is submitted */
  }
Пример #21
0
 @Override
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
   dateFormat.setLenient(false);
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
   binder.registerCustomEditor(Long.TYPE, new CustomNumberEditor(Long.class, false));
   binder.registerCustomEditor(Location.class, new AbstractModelEditor(Location.class));
 }
 @InitBinder
 public void initBinder(WebDataBinder dataBinder) {
   dataBinder.registerCustomEditor(Category.class, new CategoryPropertyEditor());
   dataBinder.registerCustomEditor(null, "descriptionList", new DescriptionPropertyEditor());
   dataBinder.registerCustomEditor(null, "question.answer", new AnswerPropertyEditor());
   dataBinder.registerCustomEditor(
       null,
       "skipRule",
       new SkipPatternPropertyEditor<FormElementSkipRule>(FormElementSkipRule.class, skipDao));
 }
 @Override
 public void initBinder(WebDataBinder binder, WebRequest request) {
   // 注册自定义的属性编辑器
   // 1、日期
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   CustomDateEditor dateEditor = new CustomDateEditor(df, true);
   // 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换
   binder.registerCustomEditor(Date.class, dateEditor);
   // 自定义的电话号码编辑器
   binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberModelEditor());
 }
Пример #24
0
  private void doBind(
      WebDataBinder binder, NativeWebRequest webRequest, boolean validate, boolean failOnErrors)
      throws Exception {

    doBind(binder, webRequest);
    if (validate) {
      binder.validate();
    }
    if (failOnErrors && binder.getBindingResult().hasErrors()) {
      throw new BindException(binder.getBindingResult());
    }
  }
Пример #25
0
  @InitBinder
  public void initBinder(WebDataBinder dataBinder) {

    // Mandatory fields
    dataBinder.setRequiredFields("nom", "prenom");

    // Replace empty strings by null (conversion errors)
    // dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

    // Set the date format to "dd/MM/yyyy"
    dataBinder.registerCustomEditor(
        Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
  }
  @Test
  public void shouldReturnFormattedDateWhenDataValue() throws Exception {

    WebDataBinder dataBinder = new WebDataBinder(null);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString = "2015-10-01 12:00:00";
    Date value = dateFormat.parse(dateString);

    controller.initBinder(dataBinder);
    CustomDateEditor editor = (CustomDateEditor) dataBinder.findCustomEditor(Date.class, null);
    editor.setValue(value);
    String parsedDate = editor.getAsText();
    assertThat(dateString, is(parsedDate));
  }
 @InitBinder
 public void initBinder(WebDataBinder binder) {
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
   dateFormat.setLenient(true);
   dateFormat2.setLenient(true);
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
   binder.registerCustomEditor(Date.class, "spacedate", new CustomDateEditor(dateFormat2, true));
   binder.registerCustomEditor(
       Date.class, "spacedateReality", new CustomDateEditor(dateFormat2, true));
   binder.registerCustomEditor(
       Date.class, "spacedate_begin", new CustomDateEditor(dateFormat2, true));
   binder.registerCustomEditor(
       Date.class, "spacedate_end", new CustomDateEditor(dateFormat2, true));
 }
 protected void validateComponent(WebDataBinder binder, MethodParameter parameter)
     throws BindException {
   boolean validateParameter = validateParameter(parameter);
   Annotation[] annotations = binder.getTarget().getClass().getAnnotations();
   for (Annotation annot : annotations) {
     if (annot.annotationType().getSimpleName().startsWith("Valid") && validateParameter) {
       Object hints = AnnotationUtils.getValue(annot);
       binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
     }
   }
   if (binder.getBindingResult().hasErrors()) {
     if (isBindExceptionRequired(binder, parameter)) {
       throw new BindException(binder.getBindingResult());
     }
   }
 }
Пример #29
0
  private Object resolvePathVariable(
      String pathVarName,
      MethodParameter methodParam,
      NativeWebRequest webRequest,
      Object handlerForInitBinderCall)
      throws Exception {

    Class<?> paramType = methodParam.getParameterType();
    if (pathVarName.length() == 0) {
      pathVarName = getRequiredParameterName(methodParam);
    }
    String pathVarValue = resolvePathVariable(pathVarName, paramType, webRequest);
    WebDataBinder binder = createBinder(webRequest, null, pathVarName);
    initBinder(handlerForInitBinderCall, pathVarName, binder, webRequest);
    return binder.convertIfNecessary(pathVarValue, paramType, methodParam);
  }
Пример #30
0
  private ModelAndView getModelAndViewSettingsPage(
      Map<String, String> parameters, SettingsForm settingsForm, MockHttpServletRequest request) {
    if (settingsForm == null) {
      settingsForm = new SettingsForm();
    }

    WebDataBinder binder = new WebDataBinder(settingsForm, "settingsform");

    if (parameters != null) {
      request.setParameters(parameters);
      binder.bind(new MutablePropertyValues(request.getParameterMap()));
    }

    SessionStatus status = new SimpleSessionStatus();
    return controller.submitSettingsPage(settingsForm, binder.getBindingResult(), status, request);
  }