简体   繁体   中英

Spring MVC Generics Object Binding/Type Conversion

I'm looking for a way to wrap my models or DTOs with a generic class to add a 'selected' Boolean property and be able to bind the object in my POST method controller.

Like this:

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
}

and use it like this:

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

But I'm stuck with the conversion...

When I call pf.getRow() it returns a plain Object , not a RowForm<Product> .

How can I implement a ConversionService/PropertyEditor to bind my posted data to my extended generic object?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM