2012-11-03 22 views
3

提交我從WebPage得出一個簡單的FormPage這樣定義:添加AjaxEventBehavior形成的按鈕,可以防止形式從檢票6.1和6.2

public FormPage() { 

    final FeedbackPanel feedback = new FeedbackPanel("feedback"); 
    add(feedback); 

    final TextField<String> entry = new TextField<String>("entry"); 

    final Button button = new Button("button"); 
    button.add(new AjaxEventBehavior("onclick") { 
     @Override 
     protected void onEvent(final AjaxRequestTarget target) { 
      System.out.println("Event"); 
     } 
    }); 

    Form<DataModel> form = new Form<User>("userForm", new CompoundPropertyModel<DataModel>(dataModel)) { 

     @Override 
     protected void onValidate() { 
      System.out.println("Validate"); 
      String entryValue = entry.getValue(); 
      if (entryValue == null || entryValue.length() == 0) { 
       error("entry value required"); 
      } 
     }; 

     @Override 
     protected void onSubmit() { 
      System.out.println("Submit"); 
      if (!hasErrors()) { 
       String entryValue = entry.getValue(); 
       if (!entryValue.equals("value")) { 
        error("entry has wrong value"); 
       } 
      } 
     }; 
    }; 

    form.add(entry); 
    form.add(button); 
    add(form); 
} 

我試圖做一些事情(在本例中只是打印到控制檯)在表單提交,所以我已經附加按鈕的onclick事件。這很好用:按鈕點擊執行操作,但現在表單沒有被提交。

我還與

form.add(new AjaxEventBehavior("onsubmit") 

試驗和此事件處理程序還可以防止表單提交。 例如,

entry.add(new AjaxEventBehavior("onclick") 

允許提交表單,但事件不涉及提交。 現在我很困惑,我怎樣才能讓我的表格提交併對這個活動進行一些操作。

回答

11

默認情況下,在Wicket 6中,附加到組件的行爲可防止發生默認組件動作。

如果你想同時觸發的行爲,你必須重寫updateAjaxRequestAttributes方法在你的行爲組件動作:

@Override 
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { 
    super.updateAjaxAttributes(attributes); 
    attributes.setAllowDefault(true); 
} 
+0

此代碼禁用事件並啓用提交。 – divanov