java 自动将其更改的值提交给域对象的 Wicket 复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4265432/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:36:44  来源:igfitidea点击:

Wicket checkbox that automatically submits its changed value to domain object

javacheckboxwicketform-submit

提问by Jonik

What's the cleanest way I can make a checkbox automatically submit the form it belongs to in Wicket? I don't want to include a submit button at all. The checkbox is backed by a boolean field in a domain object ("Account" in this case).

我可以让复选框自动提交它在 Wicket 中所属的表单的最干净的方法是什么?我根本不想包含提交按钮。该复选框由域对象(在本例中为“帐户”)中的布尔字段支持。

Simplified example with irrelevant parts omitted:

省略了不相关部分的简化示例:

EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);

PropertyModel<Boolean> model = new PropertyModel<Boolean>(accModel, "enabled");
CheckBox checkBox = new CheckBox("cb", model);
Form form = new Form("form");
form.add(checkBox);
add(form);

HTML:

HTML:

<form wicket:id="form" id="form" action="">
    <input wicket:id="cb" type="checkbox" />
</form>

Edit: To clarify, my goal is just to change the domain object's field (-> value in database too) when the checkbox is toggled. Any (clean, easy) way to achieve that would be fine. (I'm not sure if you actually need the form for this.)

编辑:澄清一下,我的目标只是在切换复选框时更改域对象的字段(-> 数据库中的值)。任何(干净,简单)的方法都可以实现。(我不确定你是否真的需要这个表格。)

回答by Jonik

Just overriding wantOnSelectionChangedNotifications() for the checkbox—even without overriding onSelectionChanged()—seems to do what I want.

只需覆盖复选框的 wantOnSelectionChangedNotifications() — 即使没有覆盖 onSelectionChanged() — 似乎都能满足我的需求。

This way you don't need the form on Java side, so the above code would become:

这样你就不需要Java端的表单,所以上面的代码将变成:

EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);

add(new CheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")){
    protected boolean wantOnSelectionChangedNotifications() {
        return true;
    }
});

Feel free to add better solutions, or a better explanation of what's going on with this approach!

随意添加更好的解决方案,或者更好地解释这种方法正在发生的事情!

Edit: On closer inspection, I guess the method's Javadocmakes it reasonably clear why this does what I wanted (emphasis mine):

编辑:仔细检查后,我想该方法的 Javadoc清楚地说明了为什么这样做是我想要的(强调我的):

If true, a roundtrip will be generated with each selection change, resulting in the model being updated(of just this component) and onSelectionChanged being called.

如果为 true,则每次选择更改都会生成往返,导致模型更新(仅此组件)并调用 onSelectionChanged。

回答by Brian Topping

While this may work, you are far better off using AjaxCheckBox. An anonymous subclass can be wired to receive events immediately as well as make changes to the UI outside the checkbox itself.

虽然这可能有效,但您最好使用AjaxCheckBox。可以连接匿名子类以立即接收事件以及对复选框本身之外的 UI 进行更改。

final WebMarkupContainer wmc = new WebMarkupContainer("wmc"); 
final EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id); 
wmc.setVisible(false); 
wmc.setOutputMarkupPlaceholderTag(true);
form.add(new AjaxCheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")) {
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        wmc.setVisible(accModel.isEnabled());
        target.addComponent(wmc);
        // .. more code to write the entity
    }
});

In this contrived example, the WebMarkupContainer would be made visible in sync with the value of the checkbox.

在这个人为的示例中,WebMarkupContainer 将与复选框的值同步可见。