Java Code Examples for org.springframework.validation.DataBinder#bind()
The following examples show how to use
org.springframework.validation.DataBinder#bind() .
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: MoneyFormattingTests.java From spring-analysis-note with MIT License | 6 votes |
@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()); }
Example 2
Source File: MoneyFormattingTests.java From spring-analysis-note with MIT License | 6 votes |
@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()); }
Example 3
Source File: MoneyFormattingTests.java From java-technology-stack with MIT License | 6 votes |
@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()); }
Example 4
Source File: MoneyFormattingTests.java From spring-analysis-note with MIT License | 6 votes |
@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()); }
Example 5
Source File: MoneyFormattingTests.java From java-technology-stack with MIT License | 6 votes |
@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()); }
Example 6
Source File: NacosConfigurationPropertiesBinder.java From nacos-spring-project with Apache License 2.0 | 5 votes |
private void doBind(Object bean, NacosConfigurationProperties properties, PropertyValues propertyValues) { ObjectUtils.cleanMapOrCollectionField(bean); DataBinder dataBinder = new DataBinder(bean); dataBinder.setAutoGrowNestedPaths(properties.ignoreNestedProperties()); dataBinder.setIgnoreInvalidFields(properties.ignoreInvalidFields()); dataBinder.setIgnoreUnknownFields(properties.ignoreUnknownFields()); dataBinder.bind(propertyValues); }
Example 7
Source File: JodaTimeFormattingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testBindDateWithoutErrorFallingBackToDateConstructor() { DataBinder binder = new DataBinder(new JodaTimeBean()); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); }
Example 8
Source File: EntityController.java From QuickProject with Apache License 2.0 | 5 votes |
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 9
Source File: JodaTimeFormattingTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testBindDateWithoutErrorFallingBackToDateConstructor() { DataBinder binder = new DataBinder(new JodaTimeBean()); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); }
Example 10
Source File: MoneyFormattingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAmountWithNumberFormat4() { FormattedMoneyHolder4 bean = new FormattedMoneyHolder4(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "010.500"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
Example 11
Source File: MoneyFormattingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); 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.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
Example 12
Source File: MoneyFormattingTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); 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.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
Example 13
Source File: DubboGenericServiceFactory.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
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 14
Source File: MoneyFormattingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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()); }
Example 15
Source File: MoneyFormattingTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testAmountWithNumberFormat4() { FormattedMoneyHolder4 bean = new FormattedMoneyHolder4(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "010.500"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
Example 16
Source File: MoneyFormattingTests.java From spring-analysis-note with MIT License | 5 votes |
@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()); }
Example 17
Source File: MoneyFormattingTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); 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.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
Example 18
Source File: DefaultConfigurationBeanBinder.java From spring-context-support with Apache License 2.0 | 5 votes |
@Override public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields, boolean ignoreInvalidFields, Object configurationBean) { DataBinder dataBinder = new DataBinder(configurationBean); // Set ignored* dataBinder.setIgnoreInvalidFields(ignoreUnknownFields); dataBinder.setIgnoreUnknownFields(ignoreInvalidFields); // Get properties under specified prefix from PropertySources // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(configurationProperties); // Bind dataBinder.bind(propertyValues); }
Example 19
Source File: DataBinderDeserializer.java From gvnix with GNU General Public License v3.0 | 4 votes |
/** * Deserializes JSON content into Map<String, String> format and then uses a * Spring {@link DataBinder} to bind the data from JSON message to JavaBean * objects. * <p/> * It is a workaround for issue * https://jira.springsource.org/browse/SPR-6731 that should be removed from * next gvNIX releases when that issue will be resolved. * * @param parser Parsed used for reading JSON content * @param ctxt Context that can be used to access information about this * deserialization activity. * * @return Deserializer value */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Object deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = parser.getCurrentToken(); MutablePropertyValues propertyValues = new MutablePropertyValues(); // Get target from DataBinder from local thread. If its a bean // collection // prepares array index for property names. Otherwise continue. DataBinder binder = (DataBinder) ThreadLocalUtil .getThreadVariable(BindingResult.MODEL_KEY_PREFIX .concat("JSON_DataBinder")); Object target = binder.getTarget(); // For DstaBinderList instances, contentTarget contains the final bean // for binding. DataBinderList is just a simple wrapper to deserialize // bean wrapper using DataBinder Object contentTarget = null; if (t == JsonToken.START_OBJECT) { String prefix = null; if (target instanceof DataBinderList) { prefix = binder.getObjectName().concat("[") .concat(Integer.toString(((Collection) target).size())) .concat("]."); // BeanWrapperImpl cannot create new instances if generics // don't specify content class, so do it by hand contentTarget = BeanUtils .instantiateClass(((DataBinderList) target) .getContentClass()); ((Collection) target).add(contentTarget); } else if (target instanceof Map) { // TODO LOGGER.warn("Map deserialization not implemented yet!"); } Map<String, String> obj = readObject(parser, ctxt, prefix); propertyValues.addPropertyValues(obj); } else { LOGGER.warn("Deserialization for non-object not implemented yet!"); return null; // TODO? } // bind to the target object binder.bind(propertyValues); // Note there is no need to validate the target object because // RequestResponseBodyMethodProcessor.resolveArgument() does it on top // of including BindingResult as Model attribute // For DAtaBinderList the contentTarget contains the final bean to // make the binding, so we must return it if (contentTarget != null) { return contentTarget; } return binder.getTarget(); }
Example 20
Source File: DataBinderBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 4 votes |
public static void main(String[] args) { Integer integer = new Integer(0); DataBinder dataBinder = new DataBinder(integer); dataBinder.setIgnoreUnknownFields(false); dataBinder.setIgnoreInvalidFields(false); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.addPropertyValue("value", 2); dataBinder.bind(propertyValues); System.out.println(integer); }