问题描述
我创建了一个自定义控件,它基本上是一个复选框.我希望复选框读取我传入的数据源的值 - 这将是一个托管 bean.我可以让复选框字段从 bean 中读取,但是当我更改复选框时我没有看到任何事情发生.看起来 bean 中的 setter 从未被调用过.
I've created a custom control that is basically a checkbox. I want the checkbox read the value of the dataSource I pass in - which would be a managed bean. I can get the checkbox field to read from the bean but I'm not seeing anything happen when I change the checkbox. It doesn't look like the setter in the bean ever gets called.
我的 bean 的关键片段是:
The key snippets of my bean are:
private boolean categoriesOn;
...
public boolean isCategoriesOn() {
System.out.println("Getting On Value");
return categoriesOn;
}
public void setCategoriesOn(boolean newValue) {
System.out.println("Setting On : " + newValue);
this.categoriesOn = newValue;
}
XPage 上的控件如下所示:
The control on the XPage looks like this:
<xp:checkBox id="flipSwitch"
styleClass="onoffswitch-checkbox"
value="${compositeData.dataSource}"
checkedValue="#{javascript:true}"
uncheckedValue="#{javascript:false}">
<xp:eventHandler event="onchange" submit="true"
refreshMode="complete">
</xp:eventHandler>
</xp:checkBox>
我使用自定义属性将 bean 传递给自定义控件:
I pass the bean to the custom control with a custom property:
<xc:crtl_toggleSwitch
dataSource="#{exhibitorInfo.categoriesOn}"
refreshID="computedField6">
</xc:crtl_toggleSwitch>
dataSource 设置为使用方法绑定.
dataSource is set to use Methodbinding.
我尝试了部分刷新和完全刷新.我只是不确定我需要做什么才能将更改后的值恢复到 bean 中.
I've tried with partial and full refresh. I'm just not sure what I need to do to get the changed value back into the bean.
感谢您的建议.
推荐答案
如 Peter 对 Per 链接到的问题的回答所示,复选框不能直接绑定到布尔值(这无疑是荒谬的).添加这些方法:
As indicated in Peter's answer on the question Per linked to, checkboxes cannot be bound directly to booleans (which is admittedly ridiculous). Add these methods:
public String getCategoriesOnAsString(){
return isCategoriesOn() ? "1" : "0";
}
public void setCategoriesOnAsString(String value){
setCategoriesOn("1".equals(value));
}
然后将您的复选框绑定到 #{exhibitorInfo.categoriesOnAsString}
,并将 checkedValue
和 uncheckedValue
设置为 "1"和
"0"
.
Then bind your checkbox to
#{exhibitorInfo.categoriesOnAsString}
, and set checkedValue
and uncheckedValue
to "1"
and "0"
, respectively.
这篇关于XPage 托管 Bean setter 不会在复选框更改时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!