问题
Similar problems are invoked in many posts in this forum; but no one has a solution that specific one, I thank you for helping me in this :
I'm using spring to develop a web application, I don't know what I should put in the path of the form:checkbox tag which inside the c:foreach one, here is my code :
<c:forEach items="${persons}" var="person" varStatus="i">
<tr>
<td><c:out value="${person.firstName}" /></td>
<td><c:out value="${person.lastName}" /></td>
<td><form:checkbox path="person.rights" value="Download"/>Download </td>
<td><form:checkbox path="person.rights" value="Delete"/>Delete </td>
</tr>
</c:forEach>
'rights
' is a list of Strings as it defined in the spring documentation, it has a getter and a setter like the other properties, my checkboxes work outside the c:foreach
tag, but when including them into this tag this exception is generated :
org.springframework.beans.NotReadablePropertyException: Invalid property 'person' of bean class [java.util.ArrayList]: Bean property 'person' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
do you have an idea about what the problem is ??
回答1:
This problem is strangely undocumented on most places. Here is an extract from the links I am posting below. The gist is that we need a static placeholder which maps to the type instead of the value of the bean. So anything inside a ${} will not work out. For this, and in the specific case of using a JSTL loop operator <c:forEach>
with s[ring form tld, we should refer to the type information in each iteration using the varStatus
attribute of the <c:forEach>
operator, just like indices of an array, and thus refer to the inner properties of the iterable collection using .
on the collection variable accessible via the outermost bean backing up the form.
For example:
<c:forEach items="${teamslist_session.teams}" var="team" varStatus="teamsLoop">
<form:input path="teams[${teamsLoop.index}].name"/>
</c:forEach>
where:
teamList_session
is the bean backing up the formteams
is the collection of beans the properties of which we need to set in thepath
attributevar
is the a reference to each member of theteams
collectionteamsLoop
is the iteration index, which is used in the line below to refer to the say,i
th element's bean's property calledname
Please refer to the following links for more information: Forum Discussion - See the last post The link provided for reference in link 1
回答2:
This Link can be useful to you. Thanks
来源:https://stackoverflow.com/questions/11478816/jsp-formcheckbox-into-a-cforeach