Results 1 to 2 of 2

Thread: Displaying ArrayList in a Spring Form

Hybrid View

  1. #1

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

    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>
      </tabl>
    </form>
    Code:
    @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?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I suggest a read of the reference guide...

    1. Don't use forEach use form:select and...
    2. A label isn't to be used for displaying (I suggest a read on HTML elements).
    3. path is the property on your command object not the referenced list.
    4. datafeeds is your reference data NOT your formObject
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •