Reputation: 1822
Looks like the modelobject of the autocompletetextfield is a string, even though it should be an employee, giving rise to exceptions about not being able to cast from string to employee. Why? And how can it be fixed?
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
}
});
Upvotes: 1
Views: 696
Reputation: 9345
The javadoc of AutoCompleteTextfield
says:
To convert input back into a non-String type you will have to provide a custom
IConverter
, either by overriding#getConverter(Class)
or by setting a suitableIConverter
on the application'sConverterLocator
.
Upvotes: 3