微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个从WebPage派生的简单FormPage,如下所示:

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")

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

解决方法:

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐