我尝试绑定一个包含 B、C 对象的通用列表,这些对象是 A 对象。
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class A<T> {
@Column(name = "type", nullable = false, updatable = false, insertable = false)
private String type;
@Column(name = "value")
private String value;
// used to populate the value in the inner class
public abstract void setCustomValue(T value);
}
@DiscriminatorValue("b")
public class B extends A<Integer>{
public void setCustomValue(Integer value);
}
@DiscriminatorValue("c")
public class C extends A<Boolean>{
public void setCustomValue(Boolean value);
}
这是速度模板和绑定形式:
公共类 AForm {
private List<A<?>> a;
public AForm() {
a = new ArrayList<A<?>>();
}
public AForm(List<A<?>> a) {
super();
this.a = a;
}
}
<form action="#springUrl("....")" method="post">
...
#foreach($a in $aList.a)
#set( $index = $velocityCount - 1 )
<tr>
<td>
#springFormInput("aList.a[$index].type" "")
</td>
<td>
#springFormInput("aList.a[$index].customValue" "")
</td>
</tr>
#end
...
<input type="submit" value="Save"/>
</form>
但是当我保存表单时,spring 会尝试实例化对象 A 而不是对象(B 或 C),具体取决于为表单中每个对象指定的类型。我想我必须进行自定义绑定,但我不知道该怎么做。
谢谢。