在没有 String 作为泛型类型的 wicket 中使用 AutoCompleteTextField [英] using AutoCompleteTextField in wicket without String as the generic type

查看:17
本文介绍了在没有 String 作为泛型类型的 wicket 中使用 AutoCompleteTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题如下:处理检票口中 AutoCompleteTextField 的 onchange 事件

我正在尝试使用带有自定义类的 AutoCompleteTextField 作为泛型类型,并添加一个 AjaxFormComponentUpdatingBehavior.我的意思是我想要一个

AutoCompleteTextFieldmyAutoComplete = ...;

然后添加一个 AjaxFormComponentUpdatingBehavior:

myAutoComplete.add(new AjaxFormComponentUpdatingBehavior("onchange") {@覆盖protected void onUpdate(AjaxRequestTarget 目标) {System.out.println("值:"+getValue());}});

问题在于,由于某种原因,添加该行为会使表单尝试使用 String 设置模型对象(即使 AutoCompleteTextField 具有 SomeClass 的泛型类型),从而在 onchange 事件触发时导致 ClassCastException.

有没有办法使用 AutoCompleteTextField 而不是 AutoCompleteTextField?我找不到任何例子.感谢您的时间!

感谢用户 biziclop 在这件事上的帮助.

解决方案

这与事件处理程序无关,它是由于您的组件中缺少模型类型设置所致.

表单组件可以从 3 个来源派生模型类型:

  1. 如果您使用的是 PropertyModelCompoundPropertyModel,请自动解决它.
  2. 接受它作为额外的构造函数参数.
  3. 通过 setType() 方法.
  4. (如果以上都不适用,默认行为是对复选框使用字符串或布尔值.)

这是你的选择,你可以选择三个中的任何一个,但我认为1比2好,比3好.

更新:您可能已经知道这一点,但是如果您的自定义类真的是自定义的,您还需要一个 IConverter 来处理 String<->Someclass 转换:您可以将它注册到应用程序中,也可以覆盖组件的 getConverter(Class<?> clazz ) 方法以返回它.

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.

解决方案

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.

这篇关于在没有 String 作为泛型类型的 wicket 中使用 AutoCompleteTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆