我的应用程序有这个结构模型:
public class Garage
List<Vehicule> vehicules;
...
public abstract class Vehicule
List<Wheel> wheels
...
public class Car extends Vehicule
public class Bike extends Vehicule
...
public class Wheel
boolean damaged
我有一个表格,其中包含一个复选框输入,让他知道车轮是否损坏:
<form id="garage"
action="#"
th:action="@{/save}"
th:object="${garage}"
method="post">
<div th:each="vehicule, i : ${garage.getVehicules()}">
<ul>
<li th:each="wheel, j : ${garage.getWheels()}">
<input type="checkbox" th:field="*{{vehicules[__${i.index}__].wheels}}" th:value="${wheel}" />
<label th:for="${#ids.next('wheels')}" th:text="${wheel.value}">Label</label>
</li>
</ul>
</div>
<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
</form>
我得到的是这个错误,因为我的车辆类别上有抽象关键字:
java.lang.InstantiationException: null 在 java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:na] 在 java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na] 在 java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na] 在 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:197)
最后一件事,这不是我真正的代码,但它是为您提供最相似的问题上下文。
这显然意味着你对抽象的处理不当。您始终可以使用/创建数据传输对象作为中间人来处理与 thymeleaf 表单之间的数据。