有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Wicket替换“仅在添加到父级的组件上”

我从Wicket 1.4.11中得到一个例外,内容如下:

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)

当我在选项卡面板中的选项卡面板中的面板中单击表单的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()方法,它只从panel类返回this。另一种方法是做PrivateCustomerTab.this.replaceWith(...),我也尝试过getParent().getParent().replaceWith(...)。这些都给了我这样一个信息:我无法替换未添加到其父级的内容

我想我一定是误解了一些关键概念。可能在将我的面板添加到其父面板之前处理了表单,这意味着我无法替换cancel_btnonSubmit()中的面板

我试着用谷歌搜索我的方式,并在我的《Wicket in Action》的副本中寻找一些关于它的信息。请帮我理解。。。谢谢


共 (2) 个答案

  1. # 1 楼答案

    这并不是真的要找到合适的面板。 看起来没问题。 这三个电话似乎找到了同一个面板

    它是关于,如果面板本身被添加到。若要将其自身替换为某个组件,需要询问父组件是否已添加到该组件。 然后,它让它的父母忘记自己,选择给定的组件作为子组件

    所以wicket基本上抱怨说面板没有添加到任何组件中

    同时,组件层次结构是否发生了变化

  2. # 2 楼答案

    我想这是一个新手的错误

    发生的情况是,在cancel_btnonSubmit()之前调用表单onSubmit()。由于第一次使用这些方法替换面板,因此第二次尝试替换同一面板时,自然不会再将其添加到父面板中

    为了解决这个问题,我将我的表单onSubmit()代码移到了我的保存按钮onSubmit()。通过这样做,只调用1onSubmit()方法,具体取决于单击的按钮