在布尔值和字符串之间转换 h:selectBooleanCheckbox 值
我有一个包含字段 creditCard
的支持 bean,该字段可以有两个从数据库填充的字符串值 y
或 n
。我想在复选框中显示它,以便 y
和 n
转换为 boolean
。
我该如何实施?我无法使用自定义转换器,因为在呈现响应时 getAsString()
返回 String
,而我需要一个 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
.
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
组件不支持自定义转换器。该属性必须是布尔值
。时期。您能做的最好的事情就是在持久层中进行转换,或者添加额外的布尔 getter/setter 来装饰原始的
y
/n
getter/setter 或只是替换完全是旧的 getter/setter。例如,然后在
中使用它。The
<h:selectBooleanCheckbox>
component does not support a custom converter. The property has to be aboolean
. Period.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.and then use it in the
<h:selectBooleanCheckbox>
instead.您可以将 BooleanConverter 用于 java 原语,这会将文本解析为托管bean 中的布尔值,位于 这里只需将代码放入 .xhtml 文件 ManagedBean 中即可
:
You can use the BooleanConverter for java primitives, this parses the text to boolean in your managedbean, at here just put in your code like this in you .xhtml file
ManagedBean:
我遇到了类似的问题,我同意之前的帖子,你应该在持久层中处理这个问题。
然而,还有其他解决方案。我的问题是:我在数据库中有 TINYINT 列,它表示布尔值真或假(0=假,1=真)。因此,我想在我的 JSF 应用程序中显示它们并作为布尔值进行处理。不幸的是,这不太可能,或者只是我没有找到合适的方法。但我的解决方案是使用 selectOneMeny 并将这些值转换为“是”或“否”,而不是使用复选框。这是代码,有类似问题的人可以使用它。
转换器:
JSF 页面:
在我的 ManagedBean 中,我创建了一个包含可能值(“是”和“否”)的列表
I had the similar problem, and I agree with previous post, you should handle this issues in persistence layer.
However, there are other solutions. My problem was next: I have TINYINT column in database which represented boolean true or false (0=false, 1=true). So, I wanted to display them and handle as a boolean in my JSF application. Unfortunately, that was not quite possible or just I didn't find a proper way. But instead using checkbox, my solution was to use selectOneMeny and to convert those values to "Yes" or "No". Here is the code, so someone with similar problem could use it.
Converter:
JSF Page:
And in my ManagedBean I created a list with possible values ("Yes" and "No")