I have a form that gets populated with data from the database.
Before is start describing my problem, some snippets:
One class:
// @Entity for areas
public class Area {
@Id
@Column(name = "area")
private String area;
@Column(name = "deleted")
private boolean deleted;
getter/setter
}
Second class
// @Entity for employees
public class Employee {
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@ManyToOne
@JoinColumn(name = "area")
private Area area;
@Column(name = "name")
private String name;
getter/setter
The method in EmployeeController called to return data to the jsp
protected String createDialog( @PathVariable("id") Long id, Model model ){
Employee employee = id == 0 ? new Employee() : employeeService.findById(id);
//return employee
model.addAttribute("employeeModel", employee );
//add data needed to create dropdown holding areas
//areaService.findAll returns a List<Area>
model.addAttribute("areas", areaService.findAll(
new Sort(
Sort.Direction.ASC,
"area"
)
));
return "employees/dialogUpdateEdit";
}
The jsp, showing the dropdown for the areas and, if no new employee is returned, the known data
<form:form action="employees/ajax" commandName="employeeModel" method="POST" id="createForm">
<table class="fullWidth">
<tr>
<td>Area</td>
<td>
<form:select path="area" items="${areas}" class="fullWidth">
</form:select>
</td>
</tr>
<tr>
<td>Employee Name</td>
<td><form:input path="name" class="fullWidth"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" id="btnSaveEmployee" class="fullWidth" />
</td>
</tr>
</table>
<!-- adding hidden field to hold id on update -->
<form:hidden path="id" />
</form:form>
The controller method doing validation and either returning some errors or not
@RequestMapping(value = "/ajax", method = RequestMethod.POST)
protected @ResponseBody ValidationResponse createOrUpdate(
@Validated @ModelAttribute("employeeModel") Employee employee,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
employeeService.createOrUpdate(employee);
}
return validate(employee, null, bindingResult);
}
For the problem:
This all works fine, dropdown is populated, data gets filled into the inputs.
But when I click the submit, I get the following error:
java.lang.IllegalStateException: Cannot convert value of type
[java.lang.String] to required type [com.whatever.Area] for property
‘area’: no matching editors or conversion strategy found
As far as I understand it, the form just submits the plain string for ‘area’ instead of binding the objects from the List.
How can I get the form to submit the object and not the string? is something wrong with my binding?
Thanks for your help!
To answer my own question and in the hope that the guy gets lot of credits who helped me without knowing 😉
I had to add a custom binder… Never heard of that before so I asked the long way first.
This link has the best and shortest tutorial on that subject I found since I knew what I was looking for:
http://empire5.com/development/binding-a-custom-object-in-spring-3/