Convert h:selectBooleanCheckbox value between boolean and String

有些话、适合烂在心里 提交于 2021-02-07 20:39:14

问题


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.


回答1:


The <h:selectBooleanCheckbox> component does not support a custom converter. The property has to be a 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. 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";
}

and then use it in the <h:selectBooleanCheckbox> instead.

<h:selectBooleanCheckbox value="#{bean.useCreditcard}" />



回答2:


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

<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(name="yourMB")
@ViewScoped
public class YourMB implements Serializable {

       private boolean booleanproperty;


    public boolean isBooleanproperty() {
        return booleanproperty;
    }
    public void setBooleanproperty(boolean booleanproperty) {
        this.booleanproperty = booleanproperty;
    }      

}    



回答3:


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:

@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:

<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")

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");
}

enter image description here



来源:https://stackoverflow.com/questions/6356871/convert-hselectbooleancheckbox-value-between-boolean-and-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!