开发者

Convert h:selectBooleanCheckbox value between boolean and String

开发者 https://www.devze.com 2023-03-12 22:58 出处:网络
I have a backing bean co开发者_开发百科ntaining 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 converte

I have a backing bean co开发者_开发百科ntaining 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.


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}" />


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

}    


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

Convert h:selectBooleanCheckbox value between boolean and String

0

精彩评论

暂无评论...
验证码 换一张
取 消