【问题标题】:Adding AjaxEventBehavior to form's button prevents form from submitting with Wicket 6.1 and 6.2将 AjaxEventBehavior 添加到表单的按钮可防止表单使用 Wicket 6.1 和 6.2 提交
【发布时间】:2012-11-03 09:10:23
【问题描述】:

我有一个简单的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);
}

我正在尝试在提交表单时做一些事情(在这个例子中只是打印到控制台),所以我在按钮的onclick 事件上附加了AjaxEventBehavior。这完美地工作:在按钮单击时执行操作,但现在没有提交表单。

我也在尝试

form.add(new AjaxEventBehavior("onsubmit")

并且此事件处理程序还阻止表单提交。 例如,

entry.add(new AjaxEventBehavior("onclick")

允许提交表单,但事件与提交无关。 现在我很困惑如何提交表单并对此事件执行一些操作。

【问题讨论】:

    标签: java ajax wicket


    【解决方案1】:

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

    如果你想同时触发行为和组件动作,你必须在你的行为中重写 updateAjaxRequestAttributes 方法:

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

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多