org.springframework.beans.propertyeditors.StringTrimmerEditor Java Examples

The following examples show how to use org.springframework.beans.propertyeditors.StringTrimmerEditor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CheckboxTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void withSingleValueAndEditor() throws Exception {
	this.bean.setName("Rob Harrop");
	this.tag.setPath("name");
	this.tag.setValue("   Rob Harrop");
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
	bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false));
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);

	int result = this.tag.doStartTag();
	assertEquals(Tag.SKIP_BODY, result);

	String output = getOutput();

	// wrap the output so it is valid XML
	output = "<doc>" + output + "</doc>";

	SAXReader reader = new SAXReader();
	Document document = reader.read(new StringReader(output));
	Element checkboxElement = (Element) document.getRootElement().elements().get(0);
	assertEquals("input", checkboxElement.getName());
	assertEquals("checkbox", checkboxElement.attribute("type").getValue());
	assertEquals("name", checkboxElement.attribute("name").getValue());
	assertEquals("checked", checkboxElement.attribute("checked").getValue());
	assertEquals("   Rob Harrop", checkboxElement.attribute("value").getValue());
}
 
Example #2
Source File: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
	accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

	accessor.setPropertyValue("set", "set1 ");
	accessor.setPropertyValue("sortedSet", "sortedSet1");
	accessor.setPropertyValue("list", "list1 ");
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().contains("set1"));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().contains("sortedSet1"));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().contains("list1"));

	accessor.setPropertyValue("list", Collections.singletonList("list1 "));
	assertTrue(target.getList().contains("list1"));
}
 
Example #3
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
	accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

	accessor.setPropertyValue("set", "set1 ");
	accessor.setPropertyValue("sortedSet", "sortedSet1");
	accessor.setPropertyValue("list", "list1 ");
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().contains("set1"));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().contains("sortedSet1"));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().contains("list1"));

	accessor.setPropertyValue("list", Collections.singletonList("list1 "));
	assertTrue(target.getList().contains("list1"));
}
 
Example #4
Source File: AbstractPropertyAccessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
	accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

	accessor.setPropertyValue("set", "set1 ");
	accessor.setPropertyValue("sortedSet", "sortedSet1");
	accessor.setPropertyValue("list", "list1 ");
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().contains("set1"));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().contains("sortedSet1"));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().contains("list1"));

	accessor.setPropertyValue("list", Collections.singletonList("list1 "));
	assertTrue(target.getList().contains("list1"));
}
 
Example #5
Source File: CheckboxTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void withSingleValueAndEditor() throws Exception {
	this.bean.setName("Rob Harrop");
	this.tag.setPath("name");
	this.tag.setValue("   Rob Harrop");
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
	bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false));
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);

	int result = this.tag.doStartTag();
	assertEquals(Tag.SKIP_BODY, result);

	String output = getOutput();

	// wrap the output so it is valid XML
	output = "<doc>" + output + "</doc>";

	SAXReader reader = new SAXReader();
	Document document = reader.read(new StringReader(output));
	Element checkboxElement = (Element) document.getRootElement().elements().get(0);
	assertEquals("input", checkboxElement.getName());
	assertEquals("checkbox", checkboxElement.attribute("type").getValue());
	assertEquals("name", checkboxElement.attribute("name").getValue());
	assertEquals("checked", checkboxElement.attribute("checked").getValue());
	assertEquals("   Rob Harrop", checkboxElement.attribute("value").getValue());
}
 
Example #6
Source File: CheckboxTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withSingleValueAndEditor() throws Exception {
	this.bean.setName("Rob Harrop");
	this.tag.setPath("name");
	this.tag.setValue("   Rob Harrop");
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
	bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false));
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);

	int result = this.tag.doStartTag();
	assertEquals(Tag.SKIP_BODY, result);

	String output = getOutput();

	// wrap the output so it is valid XML
	output = "<doc>" + output + "</doc>";

	SAXReader reader = new SAXReader();
	Document document = reader.read(new StringReader(output));
	Element checkboxElement = (Element) document.getRootElement().elements().get(0);
	assertEquals("input", checkboxElement.getName());
	assertEquals("checkbox", checkboxElement.attribute("type").getValue());
	assertEquals("name", checkboxElement.attribute("name").getValue());
	assertEquals("checked", checkboxElement.attribute("checked").getValue());
	assertEquals("   Rob Harrop", checkboxElement.attribute("value").getValue());
}
 
Example #7
Source File: BeanWrapperGenericsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
	GenericBean<String> gb = new GenericBean<String>();
	ArrayList<String[]> list = new ArrayList<String[]>();
	list.add(new String[] {"str1", "str2"});
	gb.setListOfArrays(list);
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
	bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
	assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
	assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}
 
Example #8
Source File: BaseController.java    From SpringBoot-Base-System with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 由InitBinder表示的方法,可以对WebDataBinder对象进行初始化。WebDataBinder是DataBinder的子类,
 * 用于完成由表单到JavaBean属性的绑定。 InitBinder方法不能有返回值,它必须名为void。
 * InitBinder方法的参数通常是WebDataBinder,@InitBinder可以对WebDataBinder进行初始化。
 * 
 * @time 2018年4月10日 下午5:12:31.</br>
 * @version V1.0</br>
 * @param webDataBinder</br>
 */
@InitBinder
protected void initBinder(WebDataBinder webDataBinder) {
	/**
	 * 一个用于trim 的 String类型的属性编辑器 如默认删除两边的空格,charsToDelete属性:可以设置为其他字符
	 * emptyAsNull属性:将一个空字符串转化为null值的选项。
	 */
	webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
}
 
Example #9
Source File: DubboGenericServiceFactory.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private void bindReferenceBean(ReferenceBean<GenericService> referenceBean,
		Map<String, Object> dubboTranslatedAttributes) {
	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);
				}
			});

	// ignore "registries" field and then use RegistryConfig beans
	dataBinder.setDisallowedFields("registries");

	dataBinder.bind(new MutablePropertyValues(dubboTranslatedAttributes));

	registryConfigs.ifAvailable(referenceBean::setRegistries);
}
 
Example #10
Source File: RegisteredServiceSimpleFormController.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * Sets the require fields and the disallowed fields from the
 * HttpServletRequest.
 *
 * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest,
 * org.springframework.web.bind.ServletRequestDataBinder)
 */
@Override
protected void initBinder(final HttpServletRequest request,
        final ServletRequestDataBinder binder) throws Exception {
    binder.setRequiredFields(new String[] {"description", "serviceId",
            "name", "allowedToProxy", "enabled", "ssoEnabled",
            "anonymousAccess", "evaluationOrder"});
    binder.setDisallowedFields(new String[] {"id"});
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
 
Example #11
Source File: HtmlControllerAdvice.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
    // 文字列フィールドが未入力の場合に、空文字ではなくNULLをバインドする
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

    // 文字列をIDに変換する
    binder.registerCustomEditor(ID.class, new IDTypeEditor());

    // idカラムを入力値で上書きしない
    binder.setDisallowedFields("*.id");

    // versionカラムを入力値で上書きしない
    binder.setDisallowedFields("*.version");
}
 
Example #12
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testConversionWithInappropriateStringEditor() {
	DataBinder dataBinder = new DataBinder(null);
	DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
	dataBinder.setConversionService(conversionService);
	dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

	NameBean bean = new NameBean("Fred");
	assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class));
	conversionService.addConverter(new NameBeanConverter());
	assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class));
}
 
Example #13
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testConversionWithInappropriateStringEditor() {
	DataBinder dataBinder = new DataBinder(null);
	DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
	dataBinder.setConversionService(conversionService);
	dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

	NameBean bean = new NameBean("Fred");
	assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class));
	conversionService.addConverter(new NameBeanConverter());
	assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class));
}
 
Example #14
Source File: AbstractPropertyAccessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyTypeIndexedProperty() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	assertEquals(null, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.setPropertyValue("map[key0]", "my String");
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}
 
Example #15
Source File: StringTrimmerAdvice.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the {@link StringTrimmerEditor}
 *
 * @param webDataBinder
 */
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
  StringTrimmerEditor trimmer =
      new StringTrimmerEditor(this.getCharsToDelete(), this.isEmptyAsNull());
  webDataBinder.registerCustomEditor(String.class, trimmer);
}
 
Example #16
Source File: ClinicBindingInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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));
	binder.registerCustomEditor(PetType.class, new PetTypeEditor(this.clinic));
}
 
Example #17
Source File: StartPageController.java    From podcastpedia-web with MIT License 5 votes vote down vote up
@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 */
}
 
Example #18
Source File: SearchController.java    From podcastpedia-web with MIT License 5 votes vote down vote up
@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 */		
}
 
Example #19
Source File: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void propertyTypeIndexedProperty() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	assertEquals(null, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.setPropertyValue("map[key0]", "my String");
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}
 
Example #20
Source File: BeanWrapperGenericsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
	GenericBean<String> gb = new GenericBean<>();
	ArrayList<String[]> list = new ArrayList<>();
	list.add(new String[] {"str1", "str2"});
	gb.setListOfArrays(list);
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
	bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
	assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
	assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}
 
Example #21
Source File: DataBinderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-14888
public void testSetAutoGrowCollectionLimitAfterInitialization() {
	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");

	DataBinder binder = new DataBinder(new BeanWithIntegerList());
	binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	binder.setAutoGrowCollectionLimit(257);
}
 
Example #22
Source File: DataBinderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testConversionWithInappropriateStringEditor() {
	DataBinder dataBinder = new DataBinder(null);
	DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
	dataBinder.setConversionService(conversionService);
	dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

	NameBean bean = new NameBean("Fred");
	assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class));
	conversionService.addConverter(new NameBeanConverter());
	assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class));
}
 
Example #23
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void propertyTypeIndexedProperty() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	assertEquals(null, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.setPropertyValue("map[key0]", "my String");
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));

	accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
	assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}
 
Example #24
Source File: ReferenceBeanBuilder.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@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() {

        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));

}
 
Example #25
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-14888
public void testSetAutoGrowCollectionLimitAfterInitialization() {
	DataBinder binder = new DataBinder(new BeanWithIntegerList());
	binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	assertThatIllegalStateException().isThrownBy(() ->
			binder.setAutoGrowCollectionLimit(257))
		.withMessageContaining("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
}
 
Example #26
Source File: BeanWrapperGenericsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
	GenericBean<String> gb = new GenericBean<>();
	ArrayList<String[]> list = new ArrayList<>();
	list.add(new String[] {"str1", "str2"});
	gb.setListOfArrays(list);
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
	bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
	assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
	assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}
 
Example #27
Source File: DocumentController.java    From maven-framework-project with MIT License 4 votes vote down vote up
@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
 
Example #28
Source File: EgovBindingInitializer.java    From oslits with GNU General Public License v3.0 4 votes vote down vote up
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));
}
 
Example #29
Source File: TaskController.java    From maven-framework-project with MIT License 4 votes vote down vote up
@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
 
Example #30
Source File: WorkflowController.java    From maven-framework-project with MIT License 4 votes vote down vote up
@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}