[英]Convert h:selectBooleanCheckbox value between boolean and String
I have a backing bean containing a field creditCard
which can have two string values y
or n
populated from the DB. 我有一个包含字段
creditCard
的支持bean,它可以从DB填充两个字符串值y
或n
。 I would like to display this in checkbox so that y
and n
gets converted to boolean
. 我想在复选框中显示这个,以便
y
和n
转换为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
. 我不能使用自定义转换器,因为
getAsString()
在呈现响应时返回String
,而我需要一个boolean
。
The <h:selectBooleanCheckbox>
component does not support a custom converter. <h:selectBooleanCheckbox>
组件不支持自定义转换器。 The property has to be a boolean
. 该属性必须是
boolean
。 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. 最好的办法是在持久层中进行转换,或者添加额外的布尔getter / setter来装饰原始的
y
/ n
getter / setter,或者只是替换旧的getter / setter。 Eg 例如
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";
}
and then use it in the <h:selectBooleanCheckbox>
instead. 然后在
<h:selectBooleanCheckbox>
使用它。
<h:selectBooleanCheckbox value="#{bean.useCreditcard}" />
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).
我的问题是下一个:我在数据库中有TINYINT列,表示布尔值为true或false(0 = false,1 = true)。 So, I wanted to display them and handle as a boolean in my JSF application.
所以,我想在我的JSF应用程序中显示它们并将其作为布尔值处理。 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".
但是使用复选框,我的解决方案是使用selectOneMeny并将这些值转换为“是”或“否”。 Here is the code, so someone with similar problem could use it.
这是代码,所以有类似问题的人可以使用它。
Converter: 转换器:
@FacesConverter("booleanConverter")
public class BooleanConverter implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
short number= 0;
try {
if (value.equals("Yes")) {
number= 1;
}
} catch (Exception ex) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_FATAL);
message.setSummary(MessageSelector.getMessage("error"));
message.setDetail(MessageSelector.getMessage("conversion_failed") + ex.getMessage());
throw new ConverterException(message);
}
return number;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value.toString();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
JSF Page: JSF页面:
<h:selectOneMenu id="selectOnePlayerSucc" value="#{vezbaTrening.izvedenaUspesno}" converter="booleanConverter">
<f:selectItems id="itemsPlayerSucc" value="#{trainingOverview.bool}" var="opt" itemValue="#{opt}" itemLabel="#{opt}"></f:selectItems>
And in my ManagedBean I created a list with possible values ("Yes" and "No") 在我的ManagedBean中,我创建了一个包含可能值的列表(“是”和“否”)
private List<String> bool;
public List<String> getBool() {
return bool;
}
public void setBool(List<String> bool) {
this.bool = bool;
@PostConstruct
public void init () {
...
bool = new LinkedList<>();
bool.add("Yes");
bool.add("No");
}
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 您可以将BooleanConverter用于java原语,这会将文本解析为您的managedbean中的boolean, 此处只需在您的.xhtml文件中输入这样的代码
<p:selectOneMenu id="id"
value="#{yourMB.booleanproperty}"
style="width:60px" converter="javax.faces.Boolean">
<p:ajax listener="#{yourMB.anylistener}"
update="anyIDcontrol" />
<f:selectItem itemLabel="------" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItem itemLabel="y" itemValue="true" />
<f:selectItem itemLabel="n" itemValue="false" />
</p:selectOneMenu>
ManagedBean: ManagedBean:
@ManagedBean(name="yourMB")
@ViewScoped
public class YourMB implements Serializable {
private boolean booleanproperty;
public boolean isBooleanproperty() {
return booleanproperty;
}
public void setBooleanproperty(boolean booleanproperty) {
this.booleanproperty = booleanproperty;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.