public void testBindingStringArrayToIntegerSet() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) { @Override protected Object convertElement(Object element) { return new Integer(element.toString()); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", new String[] {"10", "20", "30"}); binder.bind(pvs); assertEquals(tb.getSet(), binder.getBindingResult().getFieldValue("set")); assertTrue(tb.getSet() instanceof TreeSet); assertEquals(3, tb.getSet().size()); assertTrue(tb.getSet().contains(new Integer(10))); assertTrue(tb.getSet().contains(new Integer(20))); assertTrue(tb.getSet().contains(new Integer(30))); pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertNull(tb.getSet()); }
@InitBinder @Override protected void initBinder(WebDataBinder binder) { super.initBinder(binder); binder.registerCustomEditor(Collection.class, "userList", new CustomCollectionEditor(Collection.class) { @Override protected Object convertElement(Object element) { Long id = null; if (element instanceof String && !((String) element).equals("")) { //From the JSP 'element' will be a String try { id = Long.parseLong((String) element); } catch (NumberFormatException e) { Logger.getLogger(this.getClass()).warn("Element was " + ((String) element)); } } else if (element instanceof Long) { //From the database 'element' will be a Long id = (Long) element; } return id != null ? getUserDataService().read(id) : null; } }); }
/** * Bindings for request parameters * * @author Adelina */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { logger.debug("start - initBinder"); binder.registerCustomEditor(Set.class, "projectTeam.persons", new CustomCollectionEditor(Set.class){ protected Object convertElement(Object element){ if(element != null){ Integer personId = new Integer((String)element); logger.debug("person : convertElement for Id " + personId); Person person = null; try{ person = BLPerson.getInstance().get(personId); } catch(BusinessException e){ logger.debug("There was an error"); } logger.debug("start - initBinder"); return person; } logger.debug("start - initBinder"); return null; } }); }
/** * Registering Custom Editors to this controller for * better binding request parameters. * (More details on each registering) * * @author coni */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ logger.debug("initBinder - START"); // Custom editor for bind String[] to Set<Person> for persons property binder.registerCustomEditor(Set.class, "persons", new CustomCollectionEditor(Set.class){ protected Object convertElement( Object element ){ if (element != null){ Integer personId = new Integer((String)element); logger.debug("persons: convertElement for person: " + personId); Person person = null; try { person = BLPerson.getInstance().get(personId); } catch (BusinessException bexc) { logger.debug("Error while retrieving the person with id: ".concat(String.valueOf(personId))); } return person; } return null; } }); logger.debug("initBinder - END"); }
/** * Bindings for request parameters * * @author Adelina */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { logger.debug("start - initBinder"); binder.registerCustomEditor(Set.class, "modules", new CustomCollectionEditor(Set.class){ protected Object convertElement(Object element){ if(element != null){ Integer moduleId = new Integer((String)element); logger.debug("modules : convertElement for Id " + moduleId); Module module = null; try{ module = BLModule.getInstance().get(moduleId); } catch(BusinessException e){ logger.debug("There was an error"); } logger.debug("end - initBinder"); return module; } logger.debug("end - initBinder"); return null; } }); }
/** * Bindings for request parameters * * @author Adelina */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { logger.debug("start - initBinder"); binder.registerCustomEditor(Set.class, "persons", new CustomCollectionEditor(Set.class){ protected Object convertElement(Object element){ if(element != null){ Integer personId = new Integer((String)element); logger.debug("person : convertElement for Id " + personId); Person person = null; try{ person = BLPerson.getInstance().get(personId); } catch(BusinessException e){ logger.debug("There was an error"); } logger.debug("start - initBinder"); return person; } logger.debug("start - initBinder"); return null; } }); }
@Test public void testBindingNullToEmptyCollection() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertTrue(tb.getSet() instanceof TreeSet); assertTrue(tb.getSet().isEmpty()); }
/** * Registers a {@link CustomCollectionEditor} to translate a multiple select bind value into a sorted collection of * the proper model elements. * * @param binder The binder to register the editor into. * @param collectionClass The class of the collection. * @param propertyPath The path of the property to convert. * @param collectionModelClass The class of the model in the collection. * @param comparator The comparator to use for {@link SortedSet}s. * @param <C> The class of the collection. */ protected <C extends Collection<?>> void registerCollectionEditor(PropertyEditorRegistry binder, final Class<C> collectionClass, String propertyPath, final Class<? extends IModel> collectionModelClass, final Comparator<?> comparator) { if (collectionClass.isInstance(SortedSet.class) && comparator == null) { throw new NullPointerException("comparator cannot be null if the collection is sorted"); } binder.registerCustomEditor(collectionClass, propertyPath, new CustomCollectionEditor(collectionClass) { @Override protected Object convertElement(Object element) { IModel model; try { model = collectionModelClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } if (element != null) { Long id = Long.valueOf(element.toString()); model.setId(id); } return model; } @Override @SuppressWarnings({ "unchecked", "rawtypes" }) // Also warnings in CustomCollectionEditor, do don't mind the @SuppressWarnings protected Collection<Object> createCollection(Class<? extends Collection> clazz, int size) { if (SortedSet.class.isAssignableFrom(clazz)) { return new TreeSet(comparator); } return super.createCollection(clazz, size); } }); }
public void testBindingNullToEmptyCollection() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertTrue(tb.getSet() instanceof TreeSet); assertTrue(tb.getSet().isEmpty()); }
/** * Actually register the default editors for this registry instance. */ private void createDefaultEditors() { this.defaultEditors = new HashMap<Class<?>, PropertyEditor>(64); // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this.defaultEditors.put(Charset.class, new CharsetEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(Class[].class, new ClassArrayEditor()); this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(InputSource.class, new InputSourceEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); this.defaultEditors.put(UUID.class, new UUIDEditor()); if (zoneIdClass != null) { this.defaultEditors.put(zoneIdClass, new ZoneIdEditor()); } // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); // Default editors for primitive arrays. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); // The JDK does not contain a default editor for char! this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true)); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); // The JDK does not contain default editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); // Only register config value editors if explicitly requested. if (this.configValueEditorsActive) { StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae); this.defaultEditors.put(int[].class, sae); this.defaultEditors.put(long[].class, sae); } }
@Test public void withMultiListAndCustomEditor() throws Exception { List list = new ArrayList(); list.add(Country.COUNTRY_UK); list.add(Country.COUNTRY_AT); this.bean.setSomeList(list); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(List.class, new CustomCollectionEditor(LinkedList.class) { @Override public String getAsText() { return getValue().toString(); } }); exposeBindingResult(errors); this.tag.setPath("someList"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someList", selectElement.attribute("name").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); e = (Element) selectElement.selectSingleNode("option[@value = 'AT']"); assertEquals("AT node not selected", "selected", e.attribute("selected").getValue()); }
/** * Actually register the default editors for this registry instance. */ private void createDefaultEditors() { this.defaultEditors = new HashMap<Class<?>, PropertyEditor>(64); // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this.defaultEditors.put(Charset.class, new CharsetEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(Class[].class, new ClassArrayEditor()); this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(InputSource.class, new InputSourceEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Reader.class, new ReaderEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); this.defaultEditors.put(UUID.class, new UUIDEditor()); if (zoneIdClass != null) { this.defaultEditors.put(zoneIdClass, new ZoneIdEditor()); } // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); // Default editors for primitive arrays. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); // The JDK does not contain a default editor for char! this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true)); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); // The JDK does not contain default editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); // Only register config value editors if explicitly requested. if (this.configValueEditorsActive) { StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae); this.defaultEditors.put(int[].class, sae); this.defaultEditors.put(long[].class, sae); } }
public void testWithMultiListAndCustomEditor() throws Exception { List list = new ArrayList(); list.add(Country.COUNTRY_UK); list.add(Country.COUNTRY_AT); this.bean.setSomeList(list); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(List.class, new CustomCollectionEditor(LinkedList.class) { @Override public String getAsText() { return getValue().toString(); } }); exposeBindingResult(errors); this.tag.setPath("someList"); this.tag.setItems("${countries}"); this.tag.setItemValue("isoCode"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someList", selectElement.attribute("name").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); e = (Element) selectElement.selectSingleNode("option[@value = 'AT']"); assertEquals("AT node not selected", "selected", e.attribute("selected").getValue()); }
/** * Actually register the default editors for this registry instance. */ private void createDefaultEditors() { this.defaultEditors = new HashMap<Class<?>, PropertyEditor>(64); // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this.defaultEditors.put(Charset.class, new CharsetEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(Class[].class, new ClassArrayEditor()); this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(InputSource.class, new InputSourceEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); this.defaultEditors.put(UUID.class, new UUIDEditor()); // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); // Default editors for primitive arrays. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); // The JDK does not contain a default editor for char! this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true)); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); // The JDK does not contain default editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); // Only register config value editors if explicitly requested. if (this.configValueEditorsActive) { StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae); this.defaultEditors.put(int[].class, sae); this.defaultEditors.put(long[].class, sae); } }
/** * Set the extended <code>PropertyEditor</code> instances, which are only used * if explicitly called (i.e. {@link #parseString(Class, String)}. */ protected void setExtendedEditors() { extEditors.put(InputStream.class, new InputStreamEditor()); extEditors.put(InputSource.class, new InputSourceEditor()); extEditors.put(Class[].class, new ClassArrayEditor()); extEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); extEditors.put(TimeZone.class, new TimeZoneEditor()); extEditors.put(byte[].class, new ByteArrayPropertyEditor()); extEditors.put(char[].class, new CharArrayPropertyEditor()); extEditors.put(Properties.class, new PropertiesEditor()); extEditors.put(Set.class, new CustomCollectionEditor(Set.class)); extEditors .put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); extEditors.put(List.class, new CustomCollectionEditor(List.class)); extEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); extEditors.put(Collection.class, new CustomCollectionEditor( Collection.class)); extEditors.put(char.class, new CharacterEditor(false)); extEditors.put(Character.class, new CharacterEditor(true)); extEditors.put(boolean.class, new CustomBooleanEditor(false)); extEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); extEditors.put(short.class, new CustomNumberEditor(Short.class, false)); extEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); extEditors.put(long.class, new CustomNumberEditor(Long.class, false)); extEditors.put(float.class, new CustomNumberEditor(Float.class, false)); extEditors.put(double.class, new CustomNumberEditor(Double.class, false)); extEditors.put(Charset.class, new CharsetEditor()); extEditors.put(Currency.class, new CurrencyEditor()); extEditors.put(Class.class, new ClassEditor()); extEditors.put(URL.class, new URLEditor()); extEditors.put(UUID.class, new UUIDEditor()); extEditors.put(Pattern.class, new PatternEditor()); extEditors.put(URI.class, new URIEditor()); extEditors.put(File.class, new FileEditor()); extEditors.put(Locale.class, new LocaleEditor()); extEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); extEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); extEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); }