我有一个表格,其中填充了数据库中的数据。在开始描述我的问题之前,一些片段:
一类:
// @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,显示区域的下拉列表,如果没有返回新员工,则显示已知数据
<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:无法将类型 [java.lang.String] 的值转换为属性“区域”所需的类型 [com.whatever.Area]:找不到匹配的编辑器或转换策略
据我了解,表单只是提交“区域”的纯字符串,而不是绑定列表中的对象。
如何让表单提交对象而不是字符串?我的装订有问题吗?
谢谢你的帮助!