如何在托管bean中获取所选项的标签

时间:2015-03-06 08:13:30

标签: jsf label selectmanycheckbox

我需要你的帮助才能将列表的值变为两个变量。我的清单有说明和代码。但是,我需要将描述放在变量中,将代码放在不同的变量中,这样我才能实现这一点。

我的代码是

private String[] selectedCertificates;
private List<SelectItem> Certificates;

    public List<SelectItem> getCertificatesList(){
    Certificates = new ArrayList<SelectItem>();
    Certificates.add(new SelectItem("Certificate A","A"));
    Certificates.add(new SelectItem("Certificate B","B"));
    return bankCertificates;

}

public void setCertificates(List<SelectItem> Certificates) {
    this.Certificates = Certificates;
}
// Setters and Getters

选择商品代码:

                         <p:selectManyCheckbox id="Certificates" value="#{user.selectedCertificates}"
                                              layout="pageDirection" disabled="#{user.secondToggle}">
                            <f:selectItems value="#{user.Certificates}" var="bankCertificates"
                                           itemLabel="#{user.CertificatesString}" itemValue="#{user.CertificatesCode}"/>
                        </p:selectManyCheckbox>

在哪里可以定义描述应该是第一个值,代码应该是列表中的第二个值,我可以在页面中使用它们。

由于

2 个答案:

答案 0 :(得分:-1)

如果你的SelectItem bean上有getter(我认为你有字段描述和代码),请尝试以下方式执行操作,它存储你的ArrayList中第一个位置的对象字段。

String description = Certificates.get(0).getDescription();
String code = Certificates.get(0).getCode();

答案 1 :(得分:-1)

尝试

class SelectItem {
    private String code;
    private String description;

    SelectItem (String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode () {
        return code;
    }
    public String getDescription () {
        return description;
    }
}

这是你的主要课程

class MainClass {
    public static void main (String...arg) {
        //construct your list here using SelectItem class objects
        List<SelectItem> certificates =  = new ArrayList<SelectItem>();
        certificates.add(new SelectItem("Certificate A","A"));
        certificates.add(new SelectItem("Certificate B","B"));

        //now first read the SelectItem objects you have added to the list
        //or you can also iterate through the list, modify accordingly
        SelectItem si1 = certificates.get(0);
        //to read the code and description use the getters defined in SelectItem
        si1.getCode(); si1.getDescription();
    }
}

您还可以选择创建一个方法,您可以将要从列表中读取的索引传递给该方法。希望,这有帮助。