我正在寻找一种使用通用类包装模型或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<Product>
。
如何实现ConversionService / PropertyEditor将发布的数据绑定到扩展的通用对象?
答案 0 :(得分:0)
为什么不将抽象基类与selected属性一起使用,并为您的DTO扩展它呢?