在 wicket 中使用 AutoCompleteTextField 而不使用 String 作为通用类型
这个问题如下:handling to onchange event of AutoCompleteTextField in wicket
我正在尝试将 AutoCompleteTextField 与自定义类一起用作通用类型,并添加 AjaxFormComponentUpdatingBehavior。我的意思是我想要一个
AutoCompleteTextField<SomeClass> myAutoComplete = ...;
,然后添加一个 AjaxFormComponentUpdatingBehavior:
myAutoComplete.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println( "Value: "+getValue() );
}
});
问题是,出于某种原因,添加该行为会使表单尝试使用 String 设置模型对象(即使 AutoCompleteTextField 具有 SomeClass 的通用类型) ),当 onchange 事件触发时引发 ClassCastException。
有没有办法使用 AutoCompleteTextField
而不是 AutoCompleteTextField
?我找不到任何例子。感谢您抽出时间!
并感谢用户 biziclop 在此事上的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与事件处理程序无关,它是由于组件中缺少模型类型设置引起的。
表单组件可以从 3 个来源派生模型类型:
PropertyModel
或CompoundPropertyModel
,则自动解析它。setType()
方法。这些是您的选项,您可以选择三个中的任何一个,但我认为 1 比 2 好,而 2 又比 3 好。
更新:您可能已经知道这一点,但如果您的自定义类确实是自定义的,您还需要一个
IConverter
来处理 String<->Someclass 转换:您可以将其注册到应用程序或重写组件的getConverter(Class> clazz )
方法以返回它。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:
PropertyModel
or aCompoundPropertyModel
.setType()
method.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'sgetConverter(Class<?> clazz )
method to return it.