/** * Override default editor, if possible (since that's what we really mean to do here); * otherwise register as a custom editor. */ private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) { if (registry instanceof PropertyEditorRegistrySupport) { ((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor); } else { registry.registerCustomEditor(requiredType, editor); } }
@Override public void registerCustomEditor(@Nullable Class<?> requiredType, @Nullable String field, PropertyEditor propertyEditor) { getPropertyEditorRegistry().registerCustomEditor(requiredType, field, propertyEditor); }
@Override public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) { getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor); }
/** * Register the given custom property editor for all properties of the given type. * <p>Typically used in conjunction with the default * {@link org.springframework.beans.SimpleTypeConverter}; will work with any * TypeConverter that implements the PropertyEditorRegistry interface as well. * @param requiredType type of the property * @param propertyEditor editor to register * @see #setTypeConverter * @see org.springframework.beans.PropertyEditorRegistry#registerCustomEditor */ public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) { TypeConverter converter = getTypeConverter(); if (!(converter instanceof PropertyEditorRegistry)) { throw new IllegalStateException( "TypeConverter does not implement PropertyEditorRegistry interface: " + converter); } ((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor); }
/** * Add a custom formatter for the field type specified in {@link Formatter} class, * applying it to the specified fields only, if any, or otherwise to all fields. * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers. * @param formatter the formatter to add, generically declared for a specific type * @param fields the fields to apply the formatter to, or none if to be applied to all * @since 4.2 * @see #registerCustomEditor(Class, String, PropertyEditor) */ public void addCustomFormatter(Formatter<?> formatter, String... fields) { FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter); Class<?> fieldType = adapter.getFieldType(); if (ObjectUtils.isEmpty(fields)) { getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter); } else { for (String field : fields) { getPropertyEditorRegistry().registerCustomEditor(fieldType, field, adapter); } } }
/** * Add a custom formatter, applying it to the specified field types only, if any, * or otherwise to all fields matching the {@link Formatter}-declared type. * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers. * @param formatter the formatter to add (does not need to generically declare a * field type if field types are explicitly specified as parameters) * @param fieldTypes the field types to apply the formatter to, or none if to be * derived from the given {@link Formatter} implementation class * @since 4.2 * @see #registerCustomEditor(Class, PropertyEditor) */ public void addCustomFormatter(Formatter<?> formatter, Class<?>... fieldTypes) { FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter); if (ObjectUtils.isEmpty(fieldTypes)) { getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter); } else { for (Class<?> fieldType : fieldTypes) { getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter); } } }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor(df, true)); } }});
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
/** * Add a custom formatter, applying it to all fields matching the * {@link Formatter}-declared type. * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers. * @param formatter the formatter to add, generically declared for a specific type * @since 4.2 * @see #registerCustomEditor(Class, PropertyEditor) */ public void addCustomFormatter(Formatter<?> formatter) { FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter); getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter); }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true)); }
/** * Add a custom formatter, applying it to all fields matching the * {@link Formatter}-declared type. * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers. * @param formatter the formatter to add, generically declared for a specific type * @since 4.2 * @see #registerCustomEditor(Class, PropertyEditor) */ public void addCustomFormatter(Formatter<?> formatter) { FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter); getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter); }
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false)); } });
@Test public void collectionOfPetsWithEditor() throws Exception { this.tag.setPath("pets"); this.tag.setValue(new ItemPet("Rudiger")); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); PropertyEditorSupport editor = new ItemPet.CustomEditor(); bindingResult.getPropertyEditorRegistry().registerCustomEditor(ItemPet.class, editor); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("radio", checkboxElement.attribute("type").getValue()); assertEquals("pets", checkboxElement.attribute("name").getValue()); assertEquals("Rudiger", checkboxElement.attribute("value").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); }
@Test public void collectionOfPetsWithEditor() throws Exception { this.tag.setPath("pets"); this.tag.setValue(new ItemPet("Rudiger")); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); PropertyEditorSupport editor = new ItemPet.CustomEditor(); bindingResult.getPropertyEditorRegistry().registerCustomEditor(ItemPet.class, editor); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("pets", checkboxElement.attribute("name").getValue()); assertEquals("Rudiger", checkboxElement.attribute("value").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); }
@Test public void withMultiValueIntegerWithEditor() throws Exception { this.tag.setPath("someIntegerArray"); this.tag.setValue(" 1"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); MyIntegerEditor editor = new MyIntegerEditor(); bindingResult.getPropertyEditorRegistry().registerCustomEditor(Integer.class, editor); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); assertEquals(1, editor.count); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("someIntegerArray", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); assertEquals(" 1", checkboxElement.attribute("value").getValue()); }
@Test public void withSingleValueAndEditor() throws Exception { this.bean.setName("Rob Harrop"); this.tag.setPath("name"); this.tag.setValue(" Rob Harrop"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false)); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("name", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); assertEquals(" Rob Harrop", checkboxElement.attribute("value").getValue()); }
@Test public void withCheckedObjectValueAndEditor() throws Exception { this.tag.setPath("myFloat"); this.tag.setValue("F12.99"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); MyFloatEditor editor = new MyFloatEditor(); bindingResult.getPropertyEditorRegistry().registerCustomEditor(Float.class, editor); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertTagOpened(output); assertTagClosed(output); assertContainsAttribute(output, "name", "myFloat"); assertContainsAttribute(output, "type", "radio"); assertContainsAttribute(output, "value", "F" + getFloat().toString()); assertContainsAttribute(output, "checked", "checked"); }