I have a form that gets populated with data from the database. Before is start describing my problem, some snippets:
One class:
Second classCode:// @Entity for areas public class Area { @Id @Column(name = "area") private String area; @Column(name = "deleted") private boolean deleted; getter/setter }
The method in EmployeeController called to return data to the jspCode:// @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 jsp, showing the dropdown for the areas and, if no new employee is returned, the known dataCode: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 controller method doing validation and either returning some errors or notCode:<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>
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:Code:@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); }
As far as I understand it, the form just submits the plain string for 'area' instead of binding the objects from the List.Code: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
How do I get the string submitted for 'area' to be the appropriate object of type Area? Otherwise validation will always fail.
Thanks for your help!


Reply With Quote