gpt4 book ai didi

java - 是否可以在 Wicket 中嵌套相互独立的表单?

转载 作者:行者123 更新时间:2023-11-29 07:21:17 24 4
gpt4 key购买 nike

是否可以在 Wicket 中嵌套相互独立的表单?我想要一个带有提交按钮和取消按钮的表单。两个按钮都应将用户引导至同一页面(我们称之为 Foo)。提交按钮应该首先向服务器发送一些信息;取消按钮应该什么都不做。

这是我现有代码的真正简化版本:

Form form = new Form() {
public void onSubmit()
{
PageParameters params = new PageParameters();
params.put("DocumentID", docID);
setResponsePage(Foo.class, params);
}
};

DropDownChoice<String> ddc = new DropDownChoice<String>("name", new PropertyModel<String>(this, "nameSelection"), names);
ddc.setRequired(true);

final Button submitButton = new Button("Submit") {
public void onSubmit() { doSubmitStuff(true); }
};

final Button cancelButton = new Button("Cancel") {
public void onSubmit() { doSubmitStuff(false); }
};

form.add(ddc);
form.add(submitButton);
form.add(cancelButton);
form.add(new FeedbackPanel("validationMessages"));

问题是,我刚刚添加了一个 validator ,即使我按下取消按钮它也会触发,因为取消按钮与其他所有内容都附加到相同的表单。如果取消按钮位于单独的表单中,则可以避免这种情况。据我所知,我无法创建单独的表单,因为——由于 HTML 的结构——单独的表单将位于组件层次结构中的现有表单之下。

尽管有层次结构,我能否使表单以某种方式分开?或者我可以使用其他解决方案吗?

编辑:
作为对 Don Roby 评论的回应,这更接近于我尝试 setDefaultFormProcessing() 时代码的样子:

    Form<Object> theForm = new Form<Object>("theForm") {
public void onSubmit()
{
PageParameters params = new PageParameters();
params.put("DocumentID", docID);
setResponsePage(Foo.class, params);
}
};

final CheckBox checkbox = new CheckBox("checkbox", new PropertyModel<Boolean>(this, "something"));
checkbox.add(new PermissionsValidator());
theForm.add(checkbox);

final Button saveButton = new Button("Save") {
public void onSubmit()
{ someMethod(true); }
};
final Button cancelButton = new Button("Cancel") {
public void onSubmit()
{ someMethod(false); }
};

cancelButton.setDefaultFormProcessing(false);
theForm.add(saveButton);
theForm.add(cancelButton);
theForm.add(new FeedbackPanel("validationMessages"));

最佳答案

还有一个更简单的解决方案:在取消按钮上调用 setDefaultFormProcessing 方法,并将 false 作为参数:

cancelButton.setDefaultFormProcessing(false);

这样,点击取消按钮将绕过表单验证(和模型更新),直接调用onSubmit函数。

关于java - 是否可以在 Wicket 中嵌套相互独立的表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4109524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com