Announcement Announcement Module
Collapse
No announcement yet.
Question on use of custom property editors - problem found? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on use of custom property editors - problem found?

    I am fairly new to the Spring framework, developing a web application for internal use.

    I'd request help explaining to me whether I've missed something.

    I've registered several custom property editors using the CustomEditorConfigurer bean factory post-processor.

    It *appears* that there is no code that will suck those custom editors into the BeanWrapperImpl that is used by Spring's MVC/web piece ServletDataBinder and, ultimately, the validation piece's DataBinder.

    Am I missing something blatantly obvious? I can see that the constructor of BeanWrapperImpl will force-register certain well-defined PropertyEditors, but I can't find anywhere in the code (nor in tracing through with a debugger) where it would pick up the additional registered custom editors that I've defined in my application context.

    I added hacked my SimpleFormController's initBinder method to suck up the custom editors from the Context and BeanFactory, and once this occurs, the Binder does work as expected - that is, when binding request parameters, my custom editors are invoked as I'd expect.

    Code:
            ConfigurableApplicationContext cxt = (ConfigurableApplicationContext) getApplicationContext();
            AbstractBeanFactory abf = (AbstractBeanFactory) cxt.getBeanFactory();
            Map editors = abf.getCustomEditors();
            Iterator keySet = editors.keySet().iterator();
            while (keySet.hasNext()) {
                Class className = (Class) keySet.next();
                PropertyEditor propEdit = (PropertyEditor) editors.get(className);
                binder.registerCustomEditor(className, propEdit);
            }
    My question is - there has to be some way for the default Binder to pick up the custom property editors, as they're around in the BeanFactory for my application context. What's the bit that I'm missing here? Or is it a bug/feature request?

    Thanks in advance - Peter

  • #2
    It *appears* that there is no code that will suck those custom editors into the BeanWrapperImpl that is used by Spring's MVC/web piece ServletDataBinder and, ultimately, the validation piece's DataBinder.
    The CustomEditorConfigurers configure custom editors for the application context, not for the ServletRequestDataBinder, so you're right about this.

    The solution you're currently using should be okay, as long as the Map call to getCustomEditors() returns new editors every time; PropertyEditors are not thread-safe.

    I've noticed a couple of people lately asking for more standard a infrastructure to register custom editors in controllers. Maybe you could file a feature request in JIRA?

    Comment


    • #3
      PropertyEditors don't have to be Spring beans

      In simple use cases, the process is normally as follows:
      1. Write your custom PropertyEditor class
      2. Extend SimpleFormController
      3. In your controller, override initBinder, so that it creates (using Java's "new" operator) instances of your custom PropertyEditor and then registers them against the appropriate types/fields.
      If you wanted to define your PropertyEditors as Spring beans for some reason, I guess you could inject them into your controller (via its constructor or a setter, according to your religion) instead of using the "new" operator. You would then register them within the initBinder method as above.

      But you're right that there doesn't seem to be any way for controllers to automatically detect your custom property editors.

      Comment

      Working...
      X