看起来 autocompletetextfield 的模型对象是一个字符串,即使它应该是一个员工,这会导致无法从字符串转换为员工的异常。为什么?以及如何解决?
final DefaultCssAutocompleteTextField<Employee> field = new DefaultCssAutocompleteTextField<Employee>("field", new Model<Employee>(model.getObject().getMatch().getSupervisor())) {
@Override
protected Iterator<Employee> getChoices(String input) {
if (Strings.isEmpty(input)){
List<Employee> emptyList = Collections.emptyList();
return emptyList.iterator();
}
return supervisorDao.getAutoCompleteCapableSupervisors(input, 6).iterator();
}
};
form.add(field);
field.add(new AjaxFormSubmitBehavior(form, "onchange") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
//this generates an exception: cant cast from string to employee. why? and how can it be fixed?
Employee e = supervisorService.findOne(field.getModelObject().getId());
//do some stuff with the employee and some components
}
@Override
protected void onError(AjaxRequestTarget target) {
//do nothing
}
});