简体   繁体   English

在boolean和String之间转换h:selectBooleanCheckbox值

[英]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填充两个字符串值yn I would like to display this in checkbox so that y and n gets converted to boolean . 我想在复选框中显示这个,以便yn转换为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.

相关问题 将h:selectBooleanCheckbox值绑定到int / Integer而不是boolean / Boolean - Bind h:selectBooleanCheckbox value to int/Integer instead of boolean/Boolean 如何在ah:dataTable中的ah:dataTable中映射ah:selectBooleanCheckbox的值? - How to map the value of a h:selectBooleanCheckbox in a h:dataTable within a h:dataTable? 的价值 <h:selectBooleanCheckbox> 未根据后备bean值更新 - Value of <h:selectBooleanCheckbox> is not updated according to backing bean value 如何将JSF selectBooleanCheckbox的布尔值作为参数分配给java脚本函数? - How to assign a boolean value of an JSF selectBooleanCheckbox as parameter to a java script function? 使用h:selectBooleanCheckbox和c:if来获取a的行值 <rich:datatable> -JSF - Using h:selectBooleanCheckbox with c:if to obtain row value of a <rich:datatable> - JSF h:selectBooleanCheckbox的value属性的先决条件是什么,以呈现为HTML - What is pre-requisite for value attribute of h:selectBooleanCheckbox to be rendered to HTML 的价值 <h:selectBooleanCheckbox> 内 <p:dataTable> 提交时仍为假 - Value of <h:selectBooleanCheckbox> inside <p:dataTable> remains false on submit h:selectBooleanCheckbox valueChangeListener不起作用 - h:selectBooleanCheckbox valueChangeListener is not working h:selectBooleanCheckBox对选择的操作 - h:selectBooleanCheckBox action on select h:selectBooleanCheckbox中缺少属性 - Missing attributes in h:selectBooleanCheckbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM