@Test public void bindingNoErrorsNotIgnoreUnknown() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
@Test public void bindingNoErrors() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); assertTrue(binder.isIgnoreUnknownFields()); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); binder.bind(pvs); binder.close(); assertTrue("changed name correctly", rod.getName().equals("Rod")); assertTrue("changed age correctly", rod.getAge() == 32); Map<?, ?> m = binder.getBindingResult().getModel(); assertTrue("There is one element in map", m.size() == 2); FieldAccessBean tb = (FieldAccessBean) m.get("person"); assertTrue("Same object", tb.equals(rod)); }
@Test public void nestedBindingWithDisabledAutoGrow() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setAutoGrowNestedPaths(false); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry")); thrown.expect(NullValueInNestedPathException.class); binder.bind(pvs); }
@Test public void nestedBindingWithDefaultConversionNoErrors() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); assertTrue(binder.isIgnoreUnknownFields()); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry")); pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on")); binder.bind(pvs); binder.close(); assertEquals("Kerry", rod.getSpouse().getName()); assertTrue((rod.getSpouse()).isJedi()); }
@Test public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() { binder.initDirectFieldAccess(); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateAnnotated", "Oct 031, 2009"); binder.bind(propertyValues); assertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated")); assertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); }
@Test public void testBindLocalDateAnnotatedWithDirectFieldAccess() { binder.initDirectFieldAccess(); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateAnnotated", "Oct 31, 2009"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); }
@Test public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() { binder.initDirectFieldAccess(); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateAnnotated", "Oct -31, 2009"); binder.bind(propertyValues); assertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated")); assertEquals("Oct -31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); }
@Test public void testBindLocalDateAnnotatedWithDirectFieldAccess() { binder.initDirectFieldAccess(); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateAnnotated", "Oct 31, 2009"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); }
FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod"));
@Test public void testBindingWithFormatterAgainstFields() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(1.2), tb.getMyFloat()); assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat")); PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class); assertNotNull(editor); editor.setValue(new Float(1.4)); assertEquals("1,4", editor.getAsText()); editor = binder.getBindingResult().findEditor("myFloat", null); assertNotNull(editor); editor.setAsText("1,6"); assertEquals(new Float(1.6), editor.getValue()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test // SPR-15009 public void testSetCustomMessageCodesResolverBeforeInitializeBindingResultForDirectFieldAccess() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); DefaultMessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver(); messageCodesResolver.setPrefix("errors."); binder.setMessageCodesResolver(messageCodesResolver); binder.initDirectFieldAccess(); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.add("age", "invalid"); binder.bind(mpv); assertEquals("errors.typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); }
FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() { @Override
@Test public void testBindingErrorWithFormatterAgainstFields() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.initDirectFieldAccess(); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); } finally { LocaleContextHolder.resetLocaleContext(); } }
protected void initBinder(RequestContext context, DataBinder binder) { binder.initDirectFieldAccess(); } };
binder.initDirectFieldAccess(); PropertyEditorUtil.registerUimaFITEditors(binder); binder.setRequiredFields(mandatoryValues.toArray(new String[mandatoryValues.size()]));
private DataBinder dataBinder(String... allowedFields) { DataBinder dataBinder; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(this.validatorFactory.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); if(this.directFieldAccess) { // FYI: initBeanPropertyAccess() is the default, let's switch to direct field access instead dataBinder.initDirectFieldAccess(); // this should happen last, when everything else was set on the dataBinder already } return dataBinder; }
private DataBinder dataBinder(String... allowedFields) { DataBinder dataBinder; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(this.validatorFactory.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); if(this.directFieldAccess) { // FYI: initBeanPropertyAccess() is the default, let's switch to direct field access instead dataBinder.initDirectFieldAccess(); // this should happen last, when everything else was set on the dataBinder already } return dataBinder; }
private DataBinder dataBinder(String... allowedFields) { DataBinder dataBinder; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(this.validatorFactory.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); if(this.directFieldAccess) { // FYI: initBeanPropertyAccess() is the default, let's switch to direct field access instead dataBinder.initDirectFieldAccess(); // this should happen last, when everything else was set on the dataBinder already } return dataBinder; }
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { if (methodParameter.getParameterType().equals(Pageable.class)) { assertPageableUniqueness(methodParameter); Pageable request = getDefaultFromAnnotationOrFallback(methodParameter); ServletRequest servletRequest = (ServletRequest) webRequest.getNativeRequest(); PropertyValues propertyValues = new ServletRequestParameterPropertyValues(servletRequest, getPrefix(methodParameter), separator); DataBinder binder = new ServletRequestDataBinder(request); binder.initDirectFieldAccess(); binder.registerCustomEditor(Sort.class, new SortPropertyEditor("sort.dir", propertyValues)); binder.bind(propertyValues); if (request.getPageNumber() > 0) { request = new PageRequest(request.getPageNumber() - 1, request.getPageSize(), request.getSort()); } return request; } return UNRESOLVED; }