Wicket AutoCompleteTextField模型转换

时间:2013-03-27 06:25:30

标签: java autocomplete wicket

看起来autocompletetextfield的modelobject是一个字符串,即使它应该是一个雇员,也会导致无法从字符串转换为员工的异常。为什么?它怎么能修复?

        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
            }
        });

1 个答案:

答案 0 :(得分:3)

AutoCompleteTextfield的javadoc说:

  

要将输入转换回非String类型,您必须提供   自定义IConverter,可以通过覆盖#getConverter(Class)或   在应用程序的IConverter上设置合适的ConverterLocator