views:

954

answers:

2

I have a command object composed of primitive fields and an object field. How do I bind my form fields to the fields in the object?

I tried doing this but to no avail

<form:form commandName="course" method="POST">
     <form:input path="activity.activity"/>
         .
         .
         .
</form:form>

I get this error

org.springframework.beans.NotReadablePropertyException: 
      Invalid property 'course' of bean class

My Command class is like this

public class Course {
    private long id;
    private String owner;
    private String title;
    private List<LearningActivity> activity = new ArrayList<LearningActivity>();

    //getters and setters
}

public class LearningActivity {
private long ID;
private String activity;
    private String link;

    //getters and setters
}
+2  A: 

Two possible issues here:

  1. activity.activity is invalid (unless your getters do not correspond to your member variables) because Course.activity is a list. You need to address a particular list element - e.g. activity[0].activity. You'll also have to make sure it actually exists.

  2. Have you configured your FormController correctly? Does it pass Course instance to view as it should? Take a look at Spring MVC tutorial for an example.

If after you've fixed #1 and verified that #2 is done correctly the error doesn't go away, please post more details (FormController mapping / source).

ChssPly76
I tried doing #1 but I get an IndexOutOfBoundsException. I found this tutorial: mattfleming.com/node/134 and tried doing List<LearningActivity> acitivity = LazyList.getList(new List<LearningActivity>()); but I get NoClassDefFoundError: org/mortbay/util/LazyList. I already tried putting the commons-collection.jar in my build path and in my web-inf/lib path but somehow the app still does not find i
Jeune
You get `IndexOutOfBounds` because your list is empty. As I said above, you need to make sure that list element actually exists. Why are you using the list to begin with? Are you going to edit multiple activities on a single page? If so, you'll need to iterate over the list. If not, consider exposing current activity as a single-valued property.
ChssPly76
Got this to work! I used the Lazy List in jakarta commons to populate my list. Thanks!
Jeune
A: 

Your list either needs to be pre-populated with as many LearningActivity objects as you plan to refer to (using activity[0], activity[1], etc.) or it needs to be a lazy list. A lazy list is a list that will populate itself with empty objects when a given index is referenced.

A comment indicates that you're trying to use Apache Commons LazyList, which ought to work -- are you missing an import directive? However, as an alternative there is a Spring lazy list implementation called AutoPopulatingList.

JacobM