gpt4 book ai didi

java.lang.IllegalStateException : this kind of handler cannot be attached to multiple components

转载 作者:行者123 更新时间:2023-12-02 09:49:24 25 4
gpt4 key购买 nike

这是一个异常(exception):

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)

由以下代码引发:

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

但这没关系:

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

为什么?

谢谢。

添加:

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
}

}
}

最佳答案

基本上,您不能将同一行为实例分配给多个组件。如果你想提高可读性和可维护性,可以使用:

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

关于java.lang.IllegalStateException : this kind of handler cannot be attached to multiple components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com