0

I am trying to map a collection of objects in Spring MVC but its giving error Mapping of String is working fine but could not map a collection

org.springframework.beans.NotReadablePropertyException: Invalid property 'familyHistory[0].relation' of bean class [com.medicine.yourmedics.model.FamilyHistoryForm]: Field 'familyHistory[0].relation' does not exist

My Jsp file looks like

<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>

The familyhistoryform is a wrapper around the familyHistory class.

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;
}}

Family history pojo looks like

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;
}}

Just for testing purpose have created a controller which returns a list of familyhistory objects

@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";
}

If in the jsp I write the path for the form input as path="familyHistory" then it prints the string array of familyhistory objects in the input text

[com.medicine.yourmedics.model.FamilyHistory@472c6818, com.medicine.yourmedics.model.FamilyHistory@34662429, com.medicine.yourmedics.model.FamilyHistory@1dd01a9f, com.medicine.yourmedics.model.FamilyHistory@4983cc03]

  • I dont know what you are trying to pull off here, but why don't you have a normal form page, where the user can add familyHistory and also can you please set getters and setters. – We are Borg May 13 '15 at 09:32
  • 1 user can have multiple records of family history so need a collection to be displayed on the screen – Simrat Kaur May 13 '15 at 09:37
  • Where is your one-to-many mapping then? Which 2 classes are there? Some more information is in order. – We are Borg May 13 '15 at 09:44
  • Nice that you have a collection of elements in your GET method but that isn't going to be there anymore when you post the form. A new instance will be created without anything. You have 3 ways to solve this, instead of creating the form in your get method use a `@ModelAttribute` annotated method, this will be invoked before every request handling method or use `@SessionAttributes` to store the form in the session in between requests or configure the `WebDataBinder` to automatically expand the list with new objects. – M. Deinum May 13 '15 at 10:04
  • Post your `FamilyHistory` object fully not only the header. I also wonder why do you have a `FamilyHistoryList` while initially it is a linked list??? – M. Deinum May 13 '15 at 10:19
  • adding model attribute did not help. As i am able to map the familyhistory object as a whole and it gives me an array of objects but when i try to to familyhistory[0], its throwing exception of NotReadablePropertyException. All getters are in place and are public – Simrat Kaur May 13 '15 at 10:25
  • editted the question – Simrat Kaur May 13 '15 at 10:35
  • Have you tried an `ArrayList` instead of a `LinkedList`? – M. Deinum May 13 '15 at 10:54
  • arraylist is also not helping – Simrat Kaur May 13 '15 at 11:34

0 Answers0