当前位置: 首页>>代码示例>>Java>>正文


Java DataBinder类代码示例

本文整理汇总了Java中org.springframework.validation.DataBinder的典型用法代码示例。如果您正苦于以下问题:Java DataBinder类的具体用法?Java DataBinder怎么用?Java DataBinder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DataBinder类属于org.springframework.validation包,在下文中一共展示了DataBinder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testBindTagWithIndexedPropertiesAndCustomEditor

import org.springframework.validation.DataBinder; //导入依赖的package包/类
public void testBindTagWithIndexedPropertiesAndCustomEditor() 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()));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:27,代码来源:BindTagTests.java

示例2: testAmountAndUnit

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testAmountAndUnit() {
	MoneyHolder bean = new MoneyHolder();
	DataBinder binder = new DataBinder(bean);
	binder.setConversionService(conversionService);

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("amount", "USD 10.50");
	propertyValues.add("unit", "USD");
	binder.bind(propertyValues);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
	assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

	LocaleContextHolder.setLocale(Locale.CANADA);
	binder.bind(propertyValues);
	LocaleContextHolder.setLocale(Locale.US);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
	assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:MoneyFormattingTests.java

示例3: testAmountWithNumberFormat1

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testAmountWithNumberFormat1() {
	FormattedMoneyHolder1 bean = new FormattedMoneyHolder1();
	DataBinder binder = new DataBinder(bean);
	binder.setConversionService(conversionService);

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("amount", "$10.50");
	binder.bind(propertyValues);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

	LocaleContextHolder.setLocale(Locale.CANADA);
	binder.bind(propertyValues);
	LocaleContextHolder.setLocale(Locale.US);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:MoneyFormattingTests.java

示例4: testAmountWithNumberFormat3

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testAmountWithNumberFormat3() {
	FormattedMoneyHolder3 bean = new FormattedMoneyHolder3();
	DataBinder binder = new DataBinder(bean);
	binder.setConversionService(conversionService);

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("amount", "10%");
	binder.bind(propertyValues);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("10%", binder.getBindingResult().getFieldValue("amount"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:MoneyFormattingTests.java

示例5: testAmountWithNumberFormat5

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testAmountWithNumberFormat5() {
	FormattedMoneyHolder5 bean = new FormattedMoneyHolder5();
	DataBinder binder = new DataBinder(bean);
	binder.setConversionService(conversionService);

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("amount", "USD 10.50");
	binder.bind(propertyValues);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

	LocaleContextHolder.setLocale(Locale.CANADA);
	binder.bind(propertyValues);
	LocaleContextHolder.setLocale(Locale.US);
	assertEquals(0, binder.getBindingResult().getErrorCount());
	assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
	assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
	assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:MoneyFormattingTests.java

示例6: setUp

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Before
public void setUp() {
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.setEmbeddedValueResolver(new StringValueResolver() {
		@Override
		public String resolveStringValue(String strVal) {
			if ("${pattern}".equals(strVal)) {
				return "#,##.00";
			}
			else {
				return strVal;
			}
		}
	});
	conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
	conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
	LocaleContextHolder.setLocale(Locale.US);
	binder = new DataBinder(new TestBean());
	binder.setConversionService(conversionService);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:NumberFormattingTests.java

示例7: createAttributeFromRequestValue

import org.springframework.validation.DataBinder; //导入依赖的package包/类
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param methodParam the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
		MethodParameter methodParam, 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(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:29,代码来源:ServletModelAttributeMethodProcessor.java

示例8: testPlaceholdersErrorInNonEnumerable

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@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")));
	assertThat(target.getName()).isNull();
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:PropertySourcesPropertyValuesTests.java

示例9: testFirstCollectionPropertyWinsNestedAttributes

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testFirstCollectionPropertyWinsNestedAttributes() throws Exception {
	ListTestBean target = new ListTestBean();
	DataBinder binder = new DataBinder(target);
	Map<String, Object> first = new LinkedHashMap<String, Object>();
	first.put("list[0].description", "another description");
	Map<String, Object> second = new LinkedHashMap<String, Object>();
	second.put("list[0].name", "first name");
	second.put("list[0].description", "first description");
	second.put("list[1].name", "second name");
	second.put("list[1].description", "second description");
	this.propertySources.addFirst(new MapPropertySource("s", second));
	this.propertySources.addFirst(new MapPropertySource("f", first));
	binder.bind(new PropertySourcesPropertyValues(this.propertySources));
	target.getList();
	assertThat(target.getList()).hasSize(1);
	assertThat(target.getList().get(0).getDescription())
			.isEqualTo("another description");
	assertThat(target.getList().get(0).getName()).isNull();
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:21,代码来源:PropertySourcesPropertyValuesTests.java

示例10: createAttributeFromRequestValue

import org.springframework.validation.DataBinder; //导入依赖的package包/类
protected 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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:17,代码来源:NamespaceModelAttributeMethodProcessor.java

示例11: createAttributeFromRequestValue

import org.springframework.validation.DataBinder; //导入依赖的package包/类
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
 *
 * @param sourceValue   the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter     the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request       the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected 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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:31,代码来源:FormModelMethodArgumentResolver.java

示例12: createAttributeFromRequestValue

import org.springframework.validation.DataBinder; //导入依赖的package包/类
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered 
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected 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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:30,代码来源:FormModelMethodArgumentResolver.java

示例13: testPlaceholdersErrorInNonEnumerable

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@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());
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:17,代码来源:PropertySourcesPropertyValuesTests.java

示例14: testSave

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Test
public void testSave() throws Exception {
    request = newPost("/userform.html");
    // set updated properties first since adding them later will
    // result in multiple parameters with the same name getting sent
    User user = ((UserManager) applicationContext.getBean("userManager")).getUser("-1");
    user.setConfirmPassword(user.getPassword());
    user.setLastName("Updated Last Name");

    request.setRemoteUser(user.getUsername());

    BindingResult errors = new DataBinder(user).getBindingResult();
    c.onSubmit(user, errors, request, new MockHttpServletResponse());

    assertFalse(errors.hasErrors());
    assertNotNull(request.getSession().getAttribute("successMessages"));
}
 
开发者ID:SMVBE,项目名称:ldadmin,代码行数:18,代码来源:UserFormControllerTest.java

示例15: setUp

import org.springframework.validation.DataBinder; //导入依赖的package包/类
@Before
public void setUp() {
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.setEmbeddedValueResolver(new StringValueResolver() {
		@Override
		public String resolveStringValue(String strVal) {
			if ("${pattern}".equals(strVal)) {
				return "#,##.00";
			} else {
				return strVal;
			}
		}
	});
	conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
	conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
	LocaleContextHolder.setLocale(Locale.US);
	binder = new DataBinder(new TestBean());
	binder.setConversionService(conversionService);
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:20,代码来源:NumberFormattingTests.java


注:本文中的org.springframework.validation.DataBinder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。