java 将 AjaxEventBehavior 添加到表单的按钮会阻止表单使用 Wicket 6.1 和 6.2 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13207701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding AjaxEventBehavior to form's button prevents form from submitting with Wicket 6.1 and 6.2
提问by divanov
I have a simple FormPage
derived from WebPage
defined like this:
我有一个简单的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);
}
I'm trying to do something (in this example just printing to console) on form submission, so I've attached AjaxEventBehavior
on button's onclick
event. This works perfectly: action is performed on button click, but now the form is not being submitted.
我正在尝试在表单提交时做一些事情(在这个例子中只是打印到控制台),所以我附加AjaxEventBehavior
了按钮的onclick
事件。这很完美:点击按钮时执行操作,但现在没有提交表单。
I also was experimenting with
我也在试验
form.add(new AjaxEventBehavior("onsubmit")
and this event handler also prevents form submission. For example,
并且此事件处理程序还阻止表单提交。例如,
entry.add(new AjaxEventBehavior("onclick")
allows form to be submitted, but the event is not related to submission. Now I'm puzzled how can I have my form submitted and perform some action on this event.
允许提交表单,但事件与提交无关。现在我很困惑如何提交我的表单并对这个事件执行一些操作。
回答by Thomas
By default in Wicket 6, a behavior attached to a component prevents the default component action from happening.
默认情况下,在 Wicket 6 中,附加到组件的行为会阻止默认组件操作的发生。
If you want to trigger both the behavior and the component action you have to override the updateAjaxRequestAttributes method in your behavior:
如果您想同时触发行为和组件操作,您必须在您的行为中覆盖 updateAjaxRequestAttributes 方法:
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setAllowDefault(true);
}