绑定H:selectBooleanCheckbox值INT /整数代替布尔/布尔 [英] Bind h:selectBooleanCheckbox value to int/Integer instead of boolean/Boolean

查看:161
本文介绍了绑定H:selectBooleanCheckbox值INT /整数代替布尔/布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< H:selectBooleanCheckbox值=#{someBean.prop}> ,其中道具的类型 INT 的属性。难道真的不起作用直线距离?我一定要实现自定义转换器转换成布尔 INT ?有没有人正好有转换器code表示,在股票?我看有JSF​​ 2.0中的一些错误,prevents转换器< H:selectBooleanCheckbox> 进行处理。谁能证实?

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,战斧和Tomcat 6。

I use MyFaces 2, Tomahawk and Tomcat 6.

推荐答案

&LT; H:selectBooleanCheckbox&GT; 应,正如它的名字说,被绑定到布尔布尔属性。没有其他的。它允许一个转换属性实际上是在规范中的一个错误。它绝不会允许的。

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 重新present一个布尔状态?改变你的模型,让它成为一个fullworthy 布尔

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.

如果改变模型是不是出于某种原因的选项(第三方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);
}

和使用它来代替为&LT; H:selectBooleanCheckbox值=#{} bean.checked/&GT;

这篇关于绑定H:selectBooleanCheckbox值INT /整数代替布尔/布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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