我正在尝试在 Spring MVC 中映射对象集合,但它给出的错误 Mapping of String 工作正常,但无法映射集合
org.springframework.beans.NotReadablePropertyException:bean 类 [com.medicine.yourmedics.model.FamilyHistoryForm] 的无效属性“familyHistory[0].relation”:字段“familyHistory[0].relation”不存在
我的 Jsp 文件看起来像
<form:form action="familyhistory" modelAttribute="familyhistoryform" method="POST" name="familyHistoryForm">
<table id="tblData">
<c:forEach items="${familyhistoryform.familyHistory}" varStatus="i">
<form:input path="familyHistory[${i.index}].relation" type="text" id="relation${i.index}"/>
</c:forEach>
familyhistoryform 是familyHistory 类的包装器。
public class FamilyHistoryForm {
public List<FamilyHistory> familyHistory = new LinkedList<FamilyHistory>();
public List<FamilyHistory> getFamilyHistory() {
return familyHistory;
}
public void setFamilyHistory(List<FamilyHistory> familyHistory) {
this.familyHistory = familyHistory;
}}
家族史pojo看起来像
public class FamilyHistory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id", unique = true, nullable = false)
private int id;
private String relation;
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}}
仅出于测试目的创建了一个控制器,该控制器返回家庭历史对象列表
@RequestMapping(method = RequestMethod.GET, value = "/familyhistory")
public String viewRegistration(Map<String, Object> model,
HttpServletRequest request) {
List<FamilyHistory> familyHistoryList = new LinkedList<FamilyHistory>();
FamilyHistoryForm familyHistoryForm = new FamilyHistoryForm();
familyHistoryList.add(new FamilyHistory());
familyHistoryList.add(new FamilyHistory());
familyHistoryList.add(new FamilyHistory());
familyHistoryList.add(new FamilyHistory());
familyHistoryForm.setFamilyHistory(familyHistoryList);
model.put("familyhistoryform", familyHistoryForm);
return "familyhistory";
}
如果在 jsp 中我将表单输入的路径写为,那么它将在输入文本中path="familyHistory"
打印对象的字符串数组familyhistory
[com.medicine.yourmedics.model.FamilyHistory@472c6818, com.medicine.yourmedics.model.FamilyHistory@34662429, com.medicine.yourmedics.model.FamilyHistory@1dd01a9f, com.medicine.yourmedics.model.FamilyHistory@4983cc03]