java – Spring MVC忽略配置的PropertyEditor并改为使用构造函数

使用 Spring 3.1并给出了这样的东西:
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());
    }
}

我发现注册的属性编辑器没有被调用,除非我删除了在Thing中使用String的构造函数 – 这是对的吗?

为什么这样做并忽略已注册的编辑器?如何让它停止这样做?

解决方法

通过引入自己的构造函数,可以禁用编译器生成的默认构造函数.框架可能需要默认构造函数,以便能够实例化您的Thing.如果你真的需要自己的构造函数,你也可以提供一个没有任何参数供框架使用的版本.

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...