1

看起来 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
            }
        });
4

1 回答 1

3

的 javadocAutoCompleteTextfield说:

要将输入转换回非字符串类型,您必须提供自定义的IConverter,方法是覆盖#getConverter(Class)IConverter在应用程序的ConverterLocator.

于 2013-03-27T10:50:14.600 回答