Spring MVC ignores the configured PropertyEditor and uses the constructor instead

Using Spring 3.1 and considering things like this:

class Thing { public Thing() {} public Thing(String someProperty) {} } class ThingEditor extends PropertyEditorSupport{ @Override public void setAsText(String text) { if (text != null) { Thing thing = new Thing(text); // or by using a setter method setValue(thing); } } } class SomeController { @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Thing.class, new ThingEditor()); } } 

I found that the registered property editor is not being called unless I delete the constructor that accepts the string in Thing - is this correct?

Why is he doing this and ignoring the registered editor and how can I make him stop doing this?

+7
source share
2 answers

Introducing your own constructor, you disable the default constructor generated by the compiler. The default constructor is probably required by the infrastructure to be able to instantiate your object. If you really need your own constructor, you can also provide a version without any parameters for use in the framework.

0
source

Lock the property name when registering PropertyEditorSupport:

 @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Thing.class, "someProperty", new ThingEditor()); } 
0
source

All Articles