2012-08-01 53 views
2

我有一个填充数据库数据的表单。 之前开始描述我的问题,有些片段:JSP下拉列表不提交对象

一类:

// @Entity for areas 
public class Area { 
@Id 
@Column(name = "area") 
private String area; 

@Column(name = "deleted") 
private boolean deleted; 

getter/setter 
} 

二等

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

在EmployeeController的方法调用,数据返回到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"; 
} 

jsp,显示区域的下拉列表,如果没有n EW员工返回,已知数据

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

控制器方法做验证,要么将一些错误或不

@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); 
} 

对于这个问题: 这一切工作正常,下拉填充,数据获取填入投入。 但是当我点击提交,我得到以下错误:

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

据我了解,形式只是提交简单的字符串“区”,而不是从列表中绑定的对象。

如何获取表单提交对象而不是字符串?我的装订有问题吗?

感谢您的帮助!

+0

您可以发布您的控制器方法,它映射URL“员工/ Ajax”的? – Jason 2012-08-01 09:32:13

回答

0

我觉得选择你可以尝试这种类型的代码

<select name="area" id="area" >  
    <option value="${areas}">${areas}</option> 
</select>