wxkevin
wxkevin

Reputation: 1654

Displaying ArrayList in a Spring Form

I am using Spring 3.1.

I am trying to display an ArrayList of objects in a Spring Form within a JSP. Eventually I need to have a checkbox for each object so that the user may select a row and then press a button for some back-end action. But I can't get the data to display using a Spring form. I keep receiving an exception:

org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'datafeed[0]' of bean class [java.util.ArrayList]: Bean property 'datafeeds[0]' is not readable or has an invalid getter method: .... "

Here is the scaled down code:

<form:form method="post" commandName="datafeeds">
  <table>
     <thead>
        <tr>
           <th>Name</th>
           <th>State</th>
        </tr>
     <tbody>
        <c:forEach items="${datafeeds}" var="datafeed" varStatus="vs">
           <tr>
              <td><form:label path="datafeeds[${vs.index}].name/></td>
              <td><form:label path="datafeeds[${vs.index}].state/></td>
           </tr>
        </c:forEach>
     </tbody>
  </table>
</form>

Then the Controller:

@Controller
public class DataFeedController
{
   @Autowired
   SomeService service;

   @RequestMapping(value="/datafeed")
   public String showDataFeed(Model m) {
      List<DataFeed> datafeeds = service.list();
      m.addAttribute("datafeeds", datafeeds);
      return "datafeed";
   }
}

Specifically I guess my question is how does commandName, the variables in the forEach loop and the data from the Controller all work together? Can anybody show me using the above code?

Upvotes: 1

Views: 5918

Answers (2)

hedath
hedath

Reputation: 1

I was able to make it work using this tutorial. Basically you have to make a form class, which contains your list of datafeeds:

public class DataFeedForm {
    private List<DataFeed> dataFeeds;

    public List<DataFeed> getDataFeeds() {
        return dataFeeds;
    }

    public void setDataFeeds(List<DataFeed> dataFeeds) {
        this.dataFeeds = dataFeeds;
    }
}

And pass this form object (filled with datafeeds) to your view:

@Controller
public class DataFeedController
{
   @Autowired
   SomeService service;

   @RequestMapping(value="/datafeed")
   public String showDataFeed(Model m) {
      List<DataFeed> datafeeds = service.list();
      DataFeedForm form = new DataFeedForm();
      form.setDataFeeds(datafeeds);
      m.addAttribute("datafeedsForm", form);
      return "datafeed";
   }
}

And present the form's data in the view like this:

<form:form method="post" modelAttribute="datafeedsForm" commandName="datafeeds">
  <table>
     <thead>
        <tr>
           <th>Name</th>
           <th>State</th>
        </tr>
     <tbody>
        <c:forEach items="${datafeedsForm.datafeeds}" var="datafeed" varStatus="vs">
           <tr>
              <td><form:label path="datafeeds[${vs.index}].name"/></td>
              <td><form:label path="datafeeds[${vs.index}].state"/></td>
           </tr>
        </c:forEach>
     </tbody>
  </table>
</form>

Upvotes: 0

Petar Minchev
Petar Minchev

Reputation: 47373

datafeeds is an ArrayList. You access the elements with datafeeds.get(index), not with [index]. Using square brackets works only for arrays. Do it like this:

<c:forEach items="${datafeeds}" var="datafeed" varStatus="vs">
   <tr>
      <td><form:label path="${datafeed.name}"/></td>
      <td><form:label path="${datafeed.state}"/></td>
   </tr>
</c:forEach>

Upvotes: 3

Related Questions