Announcement Announcement Module
Collapse
No announcement yet.
JSP dropdown does not map to object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSP dropdown does not map to object

    I have a form that gets populated with data from the database. Before is start describing my problem, some snippets:

    One class:

    Code:
    // @Entity for areas
    public class Area {
    @Id
    @Column(name = "area")
    private String area;
    
    @Column(name = "deleted")
    private boolean deleted;
    
    getter/setter
    }
    Second class

    Code:
    // @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

    Code:
    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

    Code:
    <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
    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);
    }
    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:
        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 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!
    Last edited by ximarin; Aug 1st, 2012, 05:49 AM. Reason: spelling
Working...
X