【问题标题】:Spring MVC Generics Object Binding/Type ConversionSpring MVC 泛型对象绑定/类型转换
【发布时间】:2018-11-12 00:01:52
【问题描述】:

我正在寻找一种方法来使用泛型类包装我的模型或 DTO,以添加“选定”布尔属性并能够在我的 POST 方法控制器中绑定对象。

像这样:

public class RowForm<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private T model;
    private Boolean selected=false;

    public RowForm() {
        super();
    }

    public RowForm(T model) {
        super();
        this.model = model;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }

    public Boolean getSelected() {
        return selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }

}


public class ProductsForm implements Serializable{

    private static final long serialVersionUID = 1L;
    private RowForm<Product> row;

//...other stuff and getters/setters
}

并像这样使用它:

    @PostMapping ("/postProduct")
    public String POSTproduct(Model model, @ModelAttribute ProductsForm pf) 
    {
    ....
    }

但我坚持转换......

当我调用pf.getRow() 时,它返回一个普通的Object,而不是RowForm&lt;Product&gt;。

如何实现 ConversionService/PropertyEditor 以将发布的数据绑定到扩展的通用对象?

【问题讨论】:

    标签: spring spring-mvc generics type-conversion


    【解决方案1】:

    为什么不使用具有选定属性的抽象基类,并为您的 DTO 扩展它?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多