I’m trying to create a registration form. In this form there are some fields(username,password etc) and also a dropdown listbox. My problem is with this listbox.I manage to retrieve succesfully from a server all the items that will be added in the listbox and i want know to put it in this form.
More specific i have a list called lista, which contains the items i want to add in the listbox.
JSP:
<c:url var="attinto" value="/reg" />
<form:form modelAttribute="attribute" method="POST" action="${attinto}">
<table>
.
.
<tr>
<td><form:label path="researchareas">Research area:</form:label></td>
<td>
<select multiple="multiple">
<c:forEach items="${researchareas}" var="researcharea">
<option value="${researcharea}"> ${researcharea} </option>
</c:forEach>
</select>
</td>
</tr>
Controller:
@RequestMapping(value = "/reg", method = RequestMethod.GET)
public String getform(Model model) {
getAll(model);
model.addAttribute("attribute", new Reg());
return "reg";
}
I have to mention at this point that the getAll(model) is a void method similar to the following:
model.addAttribute("researchareas",lista);
Then i create a POST method to send this data.
Question:How can i add in the form the data from the list(into a listbox)?How i can take back the values that the user will select?
First of all, use
form:selectlike this:Then Spring could automatically bind corresponding attribute in Registration object:
Assuming Registration class has the following:
Though I’d name attribute the same as class or else you’d have to specify explicit names in the method parameters annotations.