using AutoCompleteTextField in wicket without String as the generic type

你。 提交于 2019-11-29 14:57:14

问题


This question follows this: handling to onchange event of AutoCompleteTextField in wicket

I'm trying to use the AutoCompleteTextField with a custom class as the generic type, and to add an AjaxFormComponentUpdatingBehavior. What I mean is I want to have a

AutoCompleteTextField<SomeClass> myAutoComplete = ...;

and after that add a AjaxFormComponentUpdatingBehavior:

myAutoComplete.add(new AjaxFormComponentUpdatingBehavior("onchange") {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            System.out.println( "Value: "+getValue() );

        }
    });

The problem is that for some reason, adding that behavior makes the form try to set the model object with a String (even though the AutoCompleteTextField has a generic type of SomeClass), causing a ClassCastException when the onchange event fires.

Is there a way to use AutoCompleteTextField without it being AutoCompleteTextField<String>? I couldn't find any example. Thanks for your time!

and thanks to the user biziclop for his help in this matter.


回答1:


This is unrelated to the event handler, it is caused by the lack of a model type set in your component.

Form components can derive the model type from 3 sources:

  1. Resolve it automatically if you're using a PropertyModel or a CompoundPropertyModel.
  2. Accept it as an additional constructor parameter.
  3. Via the setType() method.
  4. (If none of the above apply, the default behaviour is to use String, or Boolean for checkboxes.)

These are your options, you can choose any of the three, but I think 1 is better than 2, which is better than 3.

Update: You probably already know this but if your custom class is really custom, you'll also need an IConverter that handles the String<->Someclass conversions: you can either register it with the application or override your component's getConverter(Class<?> clazz ) method to return it.



来源:https://stackoverflow.com/questions/5226492/using-autocompletetextfield-in-wicket-without-string-as-the-generic-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!