PDA

View Full Version : Working with dynamic collections.



tdhaayushverma
Jun 17th, 2008, 02:26 PM
I have a JSP in which a new row can be added dynamically. I mean a user can add another row ( I am using Java script to get a new row).

Here is a snippet of my JSP:
<tr>
<td class="label">URL</td>
<td>

<div class="useError">
<spring:nestedPath path="commandPollBean.urls">
<spring:bind path="url">

<div>
<input name="url" type="radio" value="" />
<input name="url" type="text" value="Enter Poll URL" size="40" />
</div>
<div style="padding:8px 0 8px 100px;">- Or -</div>
<div><input name="url" type="radio" value="" />
<select><option>http://superlong.espn.com/poll/NFL</option>
<option>http://superlong.espn.com/poll/NBA</option>
<option>http://superlong.espn.com/poll/MLB</option></select>
</div>

<c:forEach items="${status.errorMessages}" var="errorMessage" >
<c:out value="${errorMessage}" /></br>
</c:forEach>
</spring:bind>
</spring:nestedPath>
</div>

</td>
<td class="sm"></td>
</tr>
<tr id="urlRow">
<td class="buttons centerme" colspan="3" id="add">
<span class="buttons centerme" id="addURLB1">
<a href="javascript:addURL('pollTable','urlRow', '1');">
Add Another URL
</a>
</span>
</td>
</tr>

And my form bean is:
public class PollForm implements Serializable {

private List<Url> urls = new ArrayList<Url>();
// getter and setters


And the list of type Url and the Url class is as follows:

public class Url implements Serializable {

private Integer urlID;
private String url;
// getter and setters

And I am using a SimpleFormcontroller
// constructor
public PollsController() {
super();
logger.info("PollsController constructor");

setSupportedMethods(new String[] { METHOD_GET, METHOD_POST });
setSessionForm(true);
setCommandName("commandPollBean");
setCommandClass(PollForm.class);
setFormView("CreatePoll");
// setSuccessView("Welcome");

}

But as i submit the form I get an error:
org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'urls.url' of bean class [com.wdig.polls.webapp.webform.PollForm]: Bean property 'urls.url' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Any pointers plz..

Thanks.

MECADAMIA
Jun 17th, 2008, 03:57 PM
Make sure the syntax of getter and setter are correct. I had this kind of problem before and found out it after i had a syntax errror in my setter method.

tdhaayushverma
Jun 17th, 2008, 04:03 PM
Thanks.
I checked that, and the getters and setters look Ok to me.

public class PollForm implements Serializable {

private List<Url> urls = new ArrayList<Url>();

public List<Url> getUrls() {
return urls;
}

public void setUrls(List<Url> urls) {
this.urls = urls;
}


public class Url implements Serializable {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url == null ? null : url.trim();
}

I think I am not setting the bean properly in the JSP or the controller needs to do the some kind of binding. Not sure how to do that.