java.lang.IllegalStateException:这种处理程序不能附加到多个组件
这是一个例外:java.lang.IllegalStateException:这种处理程序不能附加到多个组件,java,ajax,wicket,behavior,Java,Ajax,Wicket,Behavior,这是一个例外: java.lang.IllegalStateException: this kind of handler cannot be attached to multiple components; it is already attached to component [MarkupContainer [Component id = textField1]], but component [MarkupContainer [Component id = textField2]] wa
java.lang.IllegalStateException: this kind of handler cannot be attached to multiple components; it is already attached to component [MarkupContainer [Component id = textField1]], but component [MarkupContainer [Component id = textField2]] wants to be attached too
at org.apache.wicket.behavior.AbstractAjaxBehavior.bind(AbstractAjaxBehavior.java:70)
at org.apache.wicket.Component.add(Component.java:973)
at info.ems.wicket.page.HomePage.<init>(HomePage.java:23)
java.lang.IllegalStateException:这种处理程序不能附加到多个组件;它已附加到组件[MarkupContainer[component id=textField1]],但组件[MarkupContainer[component id=textField2]]也要附加
位于org.apache.wicket.behavior.AbstractAjaxBehavior.bind(AbstractAjaxBehavior.java:70)
位于org.apache.wicket.Component.add(Component.java:973)
在info.ems.wicket.page.HomePage.(HomePage.java:23)
由以下代码引发:
public class HomePage extends MasterPage {
public HomePage() {
AjaxEventBehavior ajaxOnClickBehavior = new AjaxEventBehavior("onClick") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
// Same behavior of both the textField1 and textField2
}
};
add(new TextField<String>("textField1", new Model<String>("Text Field 1")).add(ajaxOnClickBehavior));
add(new TextField<String>("textField2", new Model<String>("Text Field 2")).add(ajaxOnClickBehavior));
}
}
公共类主页扩展母版页{
公共网页(){
AjaxEventBehavior ajaxOnClickBehavior=新的AjaxEventBehavior(“onClick”){
私有静态最终长serialVersionUID=1L;
@凌驾
受保护的void onEvent(AjaxRequestTarget目标){
//textField1和textField2的行为相同
}
};
添加(新文本字段(“文本字段1”,新模型(“文本字段1”))。添加(ajaxOnClickBehavior));
添加(新文本字段(“文本字段2”,新模型(“文本字段2”))。添加(ajaxOnClickBehavior));
}
}
但这没关系:
public class HomePage extends MasterPage {
public HomePage() {
add(new TextField<String>("textField1", new Model<String>("Text Field 1")).add(new AjaxEventBehavior("onClick") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
// Behavior of textField1, same as textField2
}
}));
add(new TextField<String>("textField2", new Model<String>("Text Field 2")).add(new AjaxEventBehavior("onClick") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
// Behavior of textField2, same as textField1
}
}));
}
}
公共类主页扩展母版页{
公共网页(){
添加(新文本字段(“文本字段1”,新模型(“文本字段1”))。添加(新AjaxEventBehavior(“onClick”){
私有静态最终长serialVersionUID=1L;
@凌驾
受保护的void onEvent(AjaxRequestTarget目标){
//textField1的行为,与textField2相同
}
}));
添加(新文本字段(“文本字段2”),新模型(“文本字段2”))。添加(新AjaxEventBehavior(“onClick”){
私有静态最终长serialVersionUID=1L;
@凌驾
受保护的void onEvent(AjaxRequestTarget目标){
//textField2的行为,与textField1相同
}
}));
}
}
为什么?
多谢各位
增加:
public class HomePage extends MasterPage {
public HomePage() {
add(new TextField<String>("textField1", new Model<String>("Text Field 1")).add(new AjaxOnClickBehavior("onClick")));
add(new TextField<String>("textField2", new Model<String>("Text Field 2")).add(new AjaxOnClickBehavior("onClick")));
}
private class AjaxOnClickBehavior extends AjaxEventBehavior {
private static final long serialVersionUID = 1L;
public AjaxOnClickBehavior(String event) {
super(event);
}
@Override
protected void onEvent(AjaxRequestTarget target) {
// Same behavior of both the textField1 and textField2
}
}
}
公共类主页扩展母版页{
公共网页(){
添加(新文本字段(“文本字段1”,新模型(“文本字段1”))。添加(新AjaxOnClickBehavior(“onClick”));
添加(新文本字段(“文本字段2”,新模型(“文本字段2”))。添加(新AjaxOnClickBehavior(“onClick”));
}
私有类AjaxOnClickBehavior扩展了AjaxEventBehavior{
私有静态最终长serialVersionUID=1L;
公共AjaxOnClickBehavior(字符串事件){
超级(事件);
}
@凌驾
受保护的void onEvent(AjaxRequestTarget目标){
//textField1和textField2的行为相同
}
}
}
基本上,您不能将同一行为实例分配给多个组件。如果要提高可读性和可维护性,可以使用:
public class HomePage extends MasterPage {
public HomePage() {
add(new TextField<String>("textField1", new Model<String>("Text Field 1")).add(newOnClickBehavior()));
add(new TextField<String>("textField2", new Model<String>("Text Field 2")).add(newOnClickBehavior()));
}
protected AjaxEventBehavior newOnClickBehavior() {
return new AjaxEventBehavior("onClick") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
// Same behavior of both the textField1 and textField2
}
};
}
}
公共类主页扩展母版页{
公共网页(){
添加(新文本字段(“文本字段1”),新模型(“文本字段1”)。添加(newOnClickBehavior());
添加(新文本字段(“文本字段2”,新模型(“文本字段2”))。添加(newOnClickBehavior());
}
受保护的AjaxEventBehavior newOnClickBehavior(){
返回新的AjaxEventBehavior(“onClick”){
私有静态最终长serialVersionUID=1L;
@凌驾
受保护的void onEvent(AjaxRequestTarget目标){
//textField1和textField2的行为相同
}
};
}
}
Ajax行为只能附加到一个组件,因为其回调url取决于该组件。否则,单击ButtonB可能会执行ButtonA#onClick(),甚至更好,如果您多次使用一个类,请将其转换为命名的内部类,而不是匿名类。@biziclop我添加了一个代码。这就是你所说的“命名的内部类而不是匿名类”吗?@Tapas这正是他的意思。@Tapas Bose是的,尽管使用它还是上面描述的newOnClickBehavior()
方法在很大程度上取决于个人品味。如果您计划用不同的行为扩展类,则必须使用newOnClickBehavior()
哪些子体可以重写。@biziclop是,这取决于具体情况。