2012-08-01 66 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>