Refine search
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
@Test public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() { StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true); editor.setAsText("te\ns\ft"); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(" test "); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(" \n\f "); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); editor.setValue(null); assertEquals("", editor.getAsText()); }
protected void initBinder(WebDataBinder binder){ binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); dateFormat.setLenient(false); binder.setValidator(officerRegistrationValidation);
@Override protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); // Register CustomEditors for special fields dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { @Override public void setAsText(String text) throws java.lang.IllegalArgumentException { // Trim all whitespace String content = StringUtils.trimAllWhitespace(text); if (!StringUtils.hasText(content)) { // No content , ignore directly return; } // replace "=" to "," content = StringUtils.replace(content, "=", ","); // replace ":" to "," content = StringUtils.replace(content, ":", ","); // String[] to Map Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); setValue(parameters); } }); // Bind annotation attributes dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(@Nullable String text) { if (text == null) { setValue(null); } else { String value = text.trim(); if (this.charsToDelete != null) { value = StringUtils.deleteAny(value, this.charsToDelete); } if (this.emptyAsNull && "".equals(value)) { setValue(null); } else { setValue(value); } } }
@Override public void setAsText(String text) { this.count++; super.setAsText(text); } }
@Override protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); // Register CustomEditors for special fields dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { @Override public void setAsText(String text) throws java.lang.IllegalArgumentException { // Trim all whitespace String content = StringUtils.trimAllWhitespace(text); if (!StringUtils.hasText(content)) { // No content , ignore directly return; } // replace "=" to "," content = StringUtils.replace(content, "=", ","); // replace ":" to "," content = StringUtils.replace(content, ":", ","); // String[] to Map Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); setValue(parameters); } }); // Bind annotation attributes dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(@Nullable String text) { if (text == null) { setValue(null); } else { String value = text.trim(); if (this.charsToDelete != null) { value = StringUtils.deleteAny(value, this.charsToDelete); } if (this.emptyAsNull && "".equals(value)) { setValue(null); } else { setValue(value); } } }
@Override public void setAsText(String text) { super.setAsText(text); this.allProcessedValues.add(getValue()); } }
/** * Invoked on every request to provide the ability to register specific binders for Spring's binding process. * By default, we register a binder that treats empty Strings as null and a Boolean editor that supports either true * or false. If the value is passed in as null, it will treat it as false. * * @param binder */ @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); binder.registerCustomEditor(Boolean.class, new NonNullBooleanEditor()); }
@Override protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); // Register CustomEditors for special fields dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { @Override public void setAsText(String text) throws java.lang.IllegalArgumentException { // Trim all whitespace String content = StringUtils.trimAllWhitespace(text); if (!StringUtils.hasText(content)) { // No content , ignore directly return; } // replace "=" to "," content = StringUtils.replace(content, "=", ","); // replace ":" to "," content = StringUtils.replace(content, ":", ","); // String[] to Map Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); setValue(parameters); } }); // Bind annotation attributes dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); }
@Test public void testStringTrimmerEditorWithEmptyAsNull() { StringTrimmerEditor editor = new StringTrimmerEditor(true); editor.setAsText("test"); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(" test "); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(" "); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); editor.setValue(null); assertEquals("", editor.getAsText()); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(String text) { if (text == null) { setValue(null); } else { String value = text.trim(); if (this.charsToDelete != null) { value = StringUtils.deleteAny(value, this.charsToDelete); } if (this.emptyAsNull && "".equals(value)) { setValue(null); } else { setValue(value); } } }
@Override public void setAsText(String text) { super.setAsText(text); this.allProcessedValues.add(getValue()); } }
@Test // SPR-10578 public void missingRequestParamEmptyValueConvertedToNull() throws Exception { WebDataBinder binder = new WebRequestDataBinder(null); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder); request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory); assertNull(arg); }
@Override protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); // Register CustomEditors for special fields dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { @Override public void setAsText(String text) throws java.lang.IllegalArgumentException { // Trim all whitespace String content = StringUtils.trimAllWhitespace(text); if (!StringUtils.hasText(content)) { // No content , ignore directly return; } // replace "=" to "," content = StringUtils.replace(content, "=", ","); // replace ":" to "," content = StringUtils.replace(content, ":", ","); // String[] to Map Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); setValue(parameters); } }); // Bind annotation attributes dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); }
@Test public void testStringTrimmerEditorWithCharsToDelete() { StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false); editor.setAsText("te\ns\ft"); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(" test "); assertEquals("test", editor.getValue()); assertEquals("test", editor.getAsText()); editor.setAsText(""); assertEquals("", editor.getValue()); assertEquals("", editor.getAsText()); editor.setValue(null); assertEquals("", editor.getAsText()); }