在布尔值和字符串之间转换h:selectBooleanCheckbox值 [英] Convert h:selectBooleanCheckbox value between boolean and String
问题描述
我有一个包含字段creditCard
的后备bean,该字段可以具有从数据库填充的两个字符串值y
或n
.我想在复选框中显示此内容,以便将y
和n
转换为boolean
.
I have a backing bean containing a field creditCard
which can have two string values y
or n
populated from the DB. I would like to display this in checkbox so that y
and n
gets converted to boolean
.
我该如何实施?我不能使用自定义转换器,因为getAsString()
在呈现响应时返回String
,而我需要boolean
.
How can I implement it? I can't use a custom converter as getAsString()
returns String
while rendering the response whereas I need a boolean
.
推荐答案
<h:selectBooleanCheckbox>
组件不支持自定义转换器.该属性必须为boolean
.期间.
The <h:selectBooleanCheckbox>
component does not support a custom converter. The property has to be a boolean
. Period.
最好的办法是在持久层中进行转换,或者添加额外的布尔getter/setter来装饰原始的y
/n
getter/setter或完全替换旧的getter/setter.例如
Best what you can do is to do the conversion in the persistence layer or to add extra boolean getter/setter which decorates the original y
/n
getter/setter or to just replace the old getter/setter altogether. E.g.
private String useCreditcard; // I'd rather use a char, but ala.
public boolean isUseCreditcard() {
return "y".equals(useCreditcard);
}
public void setUseCreditcard(boolean useCreditcard) {
this.useCreditcard = useCreditcard ? "y" : "n";
}
,然后在<h:selectBooleanCheckbox>
中使用它.
<h:selectBooleanCheckbox value="#{bean.useCreditcard}" />
这篇关于在布尔值和字符串之间转换h:selectBooleanCheckbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!