Java – spring MVC ignores the configured propertyeditor and uses the constructor instead

Use spring 3.1 and give such things:

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 was not called unless I removed the constructor using string in thing - is that right?

Why do you do this and ignore registered editors? How to make it stop doing so?

Solution

By introducing its own constructor, you can disable the default constructor generated by the compiler The framework may need a default constructor to be able to instantiate your thing If you really need your own constructor, you can also provide a version without any parameters for the framework to use

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>