I have a Map<Course, Role> courses belonging to the command object user and I want to select a role from roles[enum array] = Role.values for each course in the keyset. I populate the entries with all the courses I want and a default role and put this in the user in the model. Below is my form, but I can’t figure out how to refer to the key (which is “course”) for the map
<form:form method="POST" commandName="user">
<table>
<tr>
<td>User Name:</td>
<td><form:input path="name" /></td>
</tr>
<c:forEach var="course" items="${courseChoices}">
<tr>
<form:radiobuttons path="courses['${course}']" items="${roles}"/>
</tr>
</c:forEach>
</table>
<input type="submit" value="save changes">
<form:errors path="*" />
</form:form>
I have a propertyeditor bound to convert Course to String and back, which simply uses the entity id.
I am getting
InvalidPropertyException:
Invalid property ‘courses[com.example.app.Course@7]’ of bean class [com.example.app.User]:
Invalid index in property path ‘courses[com.example.app.Course@7]’;
nested exception is org.springframework.beans.TypeMismatchException:
Failed to convert property value of type ‘java.lang.String’ to required type ‘com.example.app.Course’ for property ‘null’;
nested exception is java.lang.NumberFormatException: For input string: “com.example.app.Course@7”
I don’t know what is null, because I can see the Course with id 7 in my database.
Fundamentally, what is evaluating the path attribute and how does it understand the jsp context? Is this Spring expression language? I’ve been looking for resources to figure out how to pick values for my map keys from my set of roles, but I haven’t found much.
I suppose you have a getter
getId()or something similar in courses so instead ofpath="courses['${course}']"in the radiobuttons tag writepath="courses['${course.id}']"(orcourse.whateverTheIdIsCalledfor an id gettergetWhateverTheIdIsCalled())