Wicket复选框自动将其更改的值提交到域对象 [英] Wicket checkbox that automatically submits its changed value to domain object

查看:520
本文介绍了Wicket复选框自动将其更改的值提交到域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最干净的方式,我可以让一个复选框自动提交它属于Wicket的表单?我不想包括一个提交按钮。该复选框由域对象(在这种情况下为帐户)中的布尔字段支持。



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

  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:

 < form wicket:id =formid =formaction => 
< input wicket:id =cbtype =checkbox/>
< / form>

编辑:为了说明,我的目标只是更改域对象当复选框被切换时,字段(数据库中的 - >值)。任何(干净,容易)的方式来实现这将是罚款。 (我不知道你是否真的需要这个表单。)

解决方案

刚刚覆盖的checkboxOnSelectionChangedNotifications即使没有覆盖onSelectionChanged()—似乎做我想要的。



这样你不需要在Java端的形式,所以上面的代码将变成:

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

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

随时添加更好的解决方案,或更好地解释这种方法发生了什么! p>

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


如果为true,将生成一个
往返,每个
选择更改,导致
模型更新(只是这个
组件)和onSelectionChanged
被调用。



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

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:

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

解决方案

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

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

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 Javadoc makes it reasonably clear why this does what I wanted (emphasis mine):

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.

这篇关于Wicket复选框自动将其更改的值提交到域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆