Java Code Examples for org.springframework.validation.DataBinder#registerCustomEditor()

The following examples show how to use org.springframework.validation.DataBinder#registerCustomEditor() . 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: MyBeanWrapperFieldSetMapper.java    From SpringBootBucket with MIT License 6 votes vote down vote up
@Override
protected void initBinder(DataBinder binder) {
    binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (StringUtils.isNotEmpty(text)) {
                setValue(DateUtil.parseTimestamp(text));
            } else {
                setValue(null);
            }
        }

        @Override
        public String getAsText() throws IllegalArgumentException {
            Object date = getValue();
            if (date != null) {
                return DateUtil.formatTimestamp((Timestamp) date);
            } else {
                return "";
            }
        }
    });
}
 
Example 2
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 3
Source File: BindTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new ServletRequestDataBinder(tb, "tb");
	binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
		@Override
		public String getAsText() {
			return "something";
		}
	});
	Errors errors = binder.getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	// because of the custom editor getValue() should return a String
	assertTrue("Value is TestBean", status.getValue() instanceof String);
	assertTrue("Correct value", "something".equals(status.getValue()));
}
 
Example 4
Source File: BindTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new ServletRequestDataBinder(tb, "tb");
	binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
		@Override
		public String getAsText() {
			return "something";
		}
	});
	Errors errors = binder.getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	// because of the custom editor getValue() should return a String
	assertTrue("Value is TestBean", status.getValue() instanceof String);
	assertTrue("Correct value", "something".equals(status.getValue()));
}
 
Example 5
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 6
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new ServletRequestDataBinder(tb, "tb");
	binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
		@Override
		public String getAsText() {
			return "something";
		}
	});
	Errors errors = binder.getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	// because of the custom editor getValue() should return a String
	assertTrue("Value is TestBean", status.getValue() instanceof String);
	assertTrue("Correct value", "something".equals(status.getValue()));
}
 
Example 7
Source File: EntityController.java    From QuickProject with Apache License 2.0 5 votes vote down vote up
public Entity parseModel(HttpServletRequest request) {
    Object o = null;
    try {
        o = getEntityClass().newInstance();
    } catch (Exception e) {
        throw new RuntimeException("无法创建类的实例:" + getEntityClass());
    }
    DataBinder dataBinder = new DataBinder(o);
    dataBinder.registerCustomEditor(Time.class, new TimeEditor());
    MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
    dataBinder.bind(mpvs);

    return (Entity) o;
}
 
Example 8
Source File: ConfigurableContentNegotiationManagerWebMvcConfigurer.java    From spring-webmvc-support with GNU General Public License v3.0 4 votes vote down vote up
protected void configureContentNegotiationManagerFactoryBean(ContentNegotiationManagerFactoryBean factoryBean) {

        DataBinder dataBinder = new DataBinder(factoryBean);

        dataBinder.setDisallowedFields("contentNegotiationManager", "servletContext");

        dataBinder.setAutoGrowNestedPaths(true);

        dataBinder.registerCustomEditor(MediaType.class, "defaultContentType", new MediaTypePropertyEditor());

        MutablePropertyValues propertyValues = new MutablePropertyValues();

        propertyValues.addPropertyValues(this.propertyValues);

        dataBinder.bind(propertyValues);

    }