问题描述
我有一个 <h:selectBooleanCheckbox value="#{someBean.prop}">
其中 prop
是 int类型的属性代码>.真的不能马上生效吗?我是否必须实现自定义转换器才能将
Boolean
转换为 int
?有没有人碰巧有库存的转换器代码?我读到 JSF 2.0 中有一些错误会阻止处理 的转换器.谁能确认一下?
I have a <h:selectBooleanCheckbox value="#{someBean.prop}">
where prop
is a property of type int
. Is it really not working straight away? Do I have to implement a custom converter to convert Boolean
to int
? Does anyone happen to have converter code for that at stock? I read that there is some bug in JSF 2.0 that prevents converters for <h:selectBooleanCheckbox>
to be processed. Can anyone confirm that?
我使用 MyFaces 2、Tomahawk 和 Tomcat 6.
I use MyFaces 2, Tomahawk and Tomcat 6.
推荐答案
,正如它的名字所说,应该绑定到一个
boolean
或 Boolean
属性.没有其他的.它允许 converter
属性实际上是规范中的一个 错误.它不应该允许它.
The <h:selectBooleanCheckbox>
should, as its name say, be bound to a boolean
or Boolean
property. Nothing else. That it allows a converter
attribute is actually a bug in the spec. It should never have allowed it.
问题更多出在您的模型中,为什么要使用 int
来表示布尔状态?改变你的模型,让它成为一个完整的boolean
.
The problem is more in your model, why would you use an int
to represent a boolean state? Change your model to let it be a fullworthy boolean
.
如果由于某种原因(第 3 方 API、愚蠢的架构师或愚蠢的业务限制等)无法更改模型,则将模型 getter/setter 包装在支持 bean 中,如下所示
If changing the model isn't an option for some reason (a 3rd party API, a stupid architect, or stupid business restrictions, etc), then wrap the model getter/setter in the backing bean like follows
public boolean isChecked() {
return someModel.getSomeInt() != 0;
}
public void setChecked(boolean checked) {
someModel.setSomeInt(checked ? 1 : 0);
}
并将其用作 <h:selectBooleanCheckbox value="#{bean.checked}"/>
.
这篇关于将 h:selectBooleanCheckbox 值绑定到 int/Integer 而不是 boolean/Boolean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!