在布尔值和字符串之间转换 h:selectBooleanCheckbox 值

发布于 2024-11-15 16:03:04 字数 287 浏览 3 评论 0原文

我有一个包含字段 creditCard 的支持 bean,该字段可以有两个从数据库填充的字符串值 yn。我想在复选框中显示它,以便 yn 转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与君绝 2024-11-22 16:03:04

组件不支持自定义转换器。该属性必须是布尔值。时期。

您能做的最好的事情就是在持久层中进行转换,或者添加额外的布尔 getter/setter 来装饰原始的 y/n getter/setter 或只是替换完全是旧的 getter/setter。例如

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

,然后在 中使用它。

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

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}" />
深海里的那抹蓝 2024-11-22 16:03:04

您可以将 BooleanConverter 用于 java 原语,这会将文本解析为托管bean 中的布尔值,位于 这里只需将代码放入 .xhtml 文件 ManagedBean 中即可

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

       private boolean booleanproperty;


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

}    

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

}    
猫性小仙女 2024-11-22 16:03:04

我遇到了类似的问题,我同意之前的帖子,你应该在持久层中处理这个问题。
然而,还有其他解决方案。我的问题是:我在数据库中有 TINYINT 列,它表示布尔值真或假(0=假,1=真)。因此,我想在我的 JSF 应用程序中显示它们并作为布尔值进行处理。不幸的是,这不太可能,或者只是我没有找到合适的方法。但我的解决方案是使用 selectOneMeny 并将这些值转换为“是”或“否”,而不是使用复选框。这是代码,有类似问题的人可以使用它。

转换器:

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

<h:selectOneMenu id="selectOnePlayerSucc" value="#{vezbaTrening.izvedenaUspesno}" converter="booleanConverter">
  <f:selectItems id="itemsPlayerSucc" value="#{trainingOverview.bool}" var="opt" itemValue="#{opt}" itemLabel="#{opt}"></f:selectItems>

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

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文