将AjaxEventBehavior添加到表单按钮可防止使用Wicket 6.1和6.2提交表单

时间:2012-11-03 09:10:23

标签: java ajax wicket

我有一个简单的FormPage派生自WebPage,定义如下:

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

我正在尝试在表单提交时执行某些操作(在此示例中只是打印到控制台),因此我在按钮的AjaxEventBehavior事件上附加了onclick。这非常有效:按钮点击即可执行操作,但现在表单尚未提交。

我也在尝试

form.add(new AjaxEventBehavior("onsubmit")

此事件处理程序也会阻止表单提交。 例如,

entry.add(new AjaxEventBehavior("onclick")

允许提交表单,但该事件与提交无关。 现在我很困惑如何提交表格并对此活动采取一些行动。

1 个答案:

答案 0 :(得分:11)

默认情况下,在Wicket 6中,附加到组件的行为会阻止默认组件操作发生。

如果要同时触发行为和组件操作,则必须覆盖行为中的updateAjaxRequestAttributes方法:

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);
    attributes.setAllowDefault(true);
}
相关问题