本文整理汇总了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()));
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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"));
}
示例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);
}