检票口替换“仅在添加到父级的组件上”
我从 Wicket 1.4.11 读取到一个异常:
2010-11-03 17:44:51,971 [http-8080-1] 错误 org.apache.wicket.RequestCycle - 接口 org.apache.wicket.markup.html.form.IFormSubmitListener 的 onFormSubscribed 方法针对组件 [ MarkupContainer [Component id = customer]] 引发异常
org.apache.wicket.WicketRuntimeException:接口的 onFormSubscribed 方法
org.apache.wicket.markup.html.form.IFormSubmitListener 针对组件 [MarkupContainer [Component id = customer]] 引发异常 在 org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:193)
...
引起原因:java.lang.reflect.InitationTargetException 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
...
原因:java.lang.IllegalStateException:只能在已添加到其父级的组件上调用此方法。
在 org.apache.wicket.Component.replaceWith(Component.java:2804)
在 no.magge.iumb.web.crm.customers.PrivateCustomerTab$1.onSubmit(PrivateCustomerTab.java:34)
在 org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1565)
当我在 tabbedpanel 的 tabbedpanel 中单击面板中表单的 cancel_btn
时,就会发生这种情况。 ..这是cancel_btn
的代码:
public class PrivateCustomerTab extends Panel {
private static final long serialVersionUID = 16L;
protected Panel getCurrentPanel() {
return this;
}
public PrivateCustomerTab(String id, long customerId, final Panel backPanel) {
super(id);
final PrivateCustomerForm form = new PrivateCustomerForm("customer", customerId) {
private static final long serialVersionUID = 4L;
@Override
protected void onSubmit() {
System.out.println("\n\n(formsubmit) HELLO THERE MY PARENT IS: " + getParent() + "\n\n");
if (customerId!=0) {
PrivateCustomerTab.this.replaceWith(new PrivateCustomerTab("panel", customerId, backPanel));
}
}
};
add(form);
Button cancelButton = new Button("cancel_btn", new ResourceModel("cancel")) {
private static final long serialVersionUID = 18L;
@Override
public void onSubmit() {
System.out.println("\n\n(cancelsubmit) HELLO THERE MY PARENT IS: " + getParent() + "\n\n");
if (backPanel!=null) {
// PrivateCustomerTab.this.replace(backPanel);
getCurrentPanel().replaceWith(new CustomerListTab("panel"));
}
}
};
cancelButton.setVisible(backPanel!=null);
form.add(cancelButton);
}
}
我一直在尝试各种方法来获取当前面板,即我想要替换的面板。一种方法是使用 getCurrentPanel()
方法,该方法仅从面板类返回 this
。另一种方法是执行 PrivateCustomerTab.this.replaceWith(...)
,我也尝试过 getParent().getParent().replaceWith(...)
。这些都给了我这样的信息:我无法替换未添加到其父级的内容。
我想我一定误解了这里的一些关键概念。也许表单是在我的面板添加到其父面板之前处理的,这意味着我无法替换 cancel_btn
的 onSubmit()
中的面板?
我尝试过用谷歌搜索的方式,并在我的Wicket in Action副本中寻找相关内容。请帮助我理解...谢谢!
I get an exception from Wicket 1.4.11 reading:
2010-11-03 17:44:51,971 [http-8080-1] ERROR org.apache.wicket.RequestCycle - Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component [MarkupContainer [Component id = customer]] threw an exception
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface
org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component [MarkupContainer [Component id = customer]] threw an exception
at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:193)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: java.lang.IllegalStateException: This method can only be called on a component that has already been added to its parent.
at org.apache.wicket.Component.replaceWith(Component.java:2804)
at no.magge.iumb.web.crm.customers.PrivateCustomerTab$1.onSubmit(PrivateCustomerTab.java:34)
at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1565)
This happened when I clicked the cancel_btn
of a form in a panel in a tabbedpanel in a tabbedpanel... Here is the code for cancel_btn
:
public class PrivateCustomerTab extends Panel {
private static final long serialVersionUID = 16L;
protected Panel getCurrentPanel() {
return this;
}
public PrivateCustomerTab(String id, long customerId, final Panel backPanel) {
super(id);
final PrivateCustomerForm form = new PrivateCustomerForm("customer", customerId) {
private static final long serialVersionUID = 4L;
@Override
protected void onSubmit() {
System.out.println("\n\n(formsubmit) HELLO THERE MY PARENT IS: " + getParent() + "\n\n");
if (customerId!=0) {
PrivateCustomerTab.this.replaceWith(new PrivateCustomerTab("panel", customerId, backPanel));
}
}
};
add(form);
Button cancelButton = new Button("cancel_btn", new ResourceModel("cancel")) {
private static final long serialVersionUID = 18L;
@Override
public void onSubmit() {
System.out.println("\n\n(cancelsubmit) HELLO THERE MY PARENT IS: " + getParent() + "\n\n");
if (backPanel!=null) {
// PrivateCustomerTab.this.replace(backPanel);
getCurrentPanel().replaceWith(new CustomerListTab("panel"));
}
}
};
cancelButton.setVisible(backPanel!=null);
form.add(cancelButton);
}
}
I've been trying various way to get the current panel, the one I want to replace. One way is using the getCurrentPanel()
method which just returns this
from the panel class. Another way is doing PrivateCustomerTab.this.replaceWith(...)
, and I've also tried getParent().getParent().replaceWith(...)
. These all give me the message that I cannot replace something that isn't added to its parent.
I think I must be misunderstanding some key concept here. Maybe forms are processed before my panel is added to its parent, meaning I cannot replace the panel in cancel_btn
's onSubmit()
?
I've tried Googling my way and looking for something about it in my copy of Wicket in Action. Please help me understand... thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不是真正要找到合适的面板。
看来没问题。
所有三个调用似乎都找到了同一个面板。
它是关于面板本身被添加到的。要将其自身替换为 els ,组件需要询问父组件是否将其添加到其中。
然后它要求其父组件忘记自己并选择给定的组件作为子组件。
所以 wicket 基本上抱怨面板没有添加到任何组件中。
同时组件层次结构是否发生了变化?
It is not really about finding the right panel.
That seems to be ok.
All three calls do seem to find the same panel.
It is about, were the panel itself is added to. To replace itself with something els a component needs to ask the parent, were it is added to.
Then it ask its parent to forget about itself and choose the given Component as a child.
So wicket basically complains that the panel is not added to any component.
Was the component hierachy changed in the mean time?
我想,解决了这个问题——新手错误(再次)。
发生的情况是,表单
onSubmit()
在cancel_btn
的onSubmit()
之前被调用。由于第一个这些方法替换了面板,自然地,当第二次尝试替换同一面板时,它不再添加到父级中。为了解决这个问题,我将表单
onSubmit()
代码移至我的保存按钮onSubmit()
中。通过这样做,仅调用 1 个onSubmit()
方法,具体取决于单击的按钮。Figured this one out - newbie mistake (again), I guess.
What happens is that the forms
onSubmit()
is called before thecancel_btn
'sonSubmit()
. Since the first these methods replaces the panel, naturally the second time one tries to replace that same panel it's not added to a parent any more.To solve it I moved my forms
onSubmit()
code into the my save buttonsonSubmit()
. By doing this there is only 1onSubmit()
method called, depending on what button was clicked.